2012-10-29 12 views
5

Mam UITableView z UISwitchs na nich.Wybierz wiersz UITableView po kliknięciu UISwitch

TableView

Gdy przełącznik jest przełączana Chcę uruchomić funkcję. Funkcja rejestruje się tylko wtedy, gdy przełącznik jest włączony lub wyłączony oraz wiersz, w którym przełącznik został zmieniony. Problem polegający na tym, że po kliknięciu przełącznika nie loguję prawidłowego wiersza, chyba że kliknąłem na ten wiersz przed kliknięciem przełącznika.

Domyślam się, że mój problem polega na tym, że kliknięcie przełącznika nie powoduje wyboru wiersza. Jak mogę to zrobić, aby wybrał wiersz lub czy mogę dodać identyfikator do przełącznika?

Tak więc identyfikator przełącznika "1" to "WŁ".

Odpowiedz

10

Aby znaleźć komórkę, która posiada przełącznik

UISwitch *switchInCell = (UISwitch *)sender; 
UITableViewCell * cell = (UITableViewCell*) swithInCell.superview; 

Aby znaleźć indexpath tej komórce

NSIndexPath * indexpath = [myTableView indexPathForCell:cell] 

w twojej sprawa

- (void) switchChanged:(id)sender { 

     UISwitch *switchInCell = (UISwitch *)sender; 
     UITableViewCell * cell = (UITableViewCell*) swithInCell.superview; 
     NSIndexPath * indexpath = [myTableView indexPathForCell:cell] 
     NSString *strCatID =[[NSString alloc]init]; 
     strCatID = [self.catIDs objectAtIndex:indexpath]; 
     NSLog(@"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF"); 
     } 
+3

W iOS7 superview będzie UITableViewCellContentView, który jest zawijany w UITableViewCellScrollView. Zamiast tego można uzyskać ścieżkę indeksu komórki, używając: CGPoint switchPositionPoint = [sender convertPoint: CGPointZero toView: [self tableView]]; NSIndexPath * indexPath = [[self tableView] indexPathForRowAtPoint: switchPositionPoint]; – NSDestr0yer

1

Myślę, że problem polega na tym, że używasz dequeueReusableCellWithIdentifier, a cała twoja komórka ma ten sam identyfikator.

+0

Jeśli używam [self.inputTableView indexPathForSelectedRow] .row Id daje mi wybrany wiersz. – Will

1

Lepszym sposobem na to jest określenie, które komórka nadawca jest w.

- (UITableViewCell *)findCellForView:(UIView *)view 
{ 
    for (; view != nil; view = view.superview) 
     if ([view isKindOfClass:[UITableViewCell class]]) 
      return view; 
    return nil; 
} 

Po tej metody. to jest to kwestia zastąpienia [self.inputTableView indexPathForSelectedRow] z

UITableViewCell *cell = [self findCellForView:sender]; 
NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell]; 
9

Należy ustawić IndexPath.row jako tag do każdego Switch w cellForRowAtIndexPath Metoda

switchView.tag= indexPath.row; 

a gdy zmiana wartości przełącznik .you'll uzyskać numer wiersza

- (void) switchChanged:(UISwitch *)sender { 
    int rowIndex =[sender tag]; 
    //rowIndex you may use it further as you wanted. 

}

+0

Myślę, że jest to zdecydowanie najlepsze podejście. Inne podejścia do szukania hierarchii widoków, a nawet jeszcze gorsze, tłumaczenie punktu do widoku, poczucie hackish i jak dużo pracy. Jedynym problemem, który możesz napotkać w tym rozwiązaniu, jest to, że masz wiele sekcji, ale można to rozwiązać, powodując, że znacznik koduje zarówno sekcję, jak i wiersz. Coś takiego: switchView.tag = indexPath.section * 1000 + indexPath.row; –

4

Musiałem zrobić podwójne mySwitch.superview.superview, aby uzyskać właściwą komórkę.

Oto przykład

- (void)switchToggle:(UISwitch *)mySwitch 
{ 

UITableViewCell *cell = (UITableViewCell *)mySwitch.superview.superview; 
NSIndexPath *indexpath = [self.tableView indexPathForCell:cell]; 
NSLog(@"toggle section %d rowID %d", indexpath.section, indexpath.row); 

} 
-1
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
@property (retain, nonatomic) IBOutlet UITableView *tableview; 

@end 


@interface ViewController() 
{ 
    NSMutableArray *arr; 
    UITableViewCell* aCell; 
    UISwitch *myswitch; 
} 


#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *arr; 
    UITableViewCell* aCell; 
    UISwitch *myswitch; 
} 

@end 

@implementation ViewController 
@synthesize tableview; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
{ 
    return [arr count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    aCell = [tableview dequeueReusableCellWithIdentifier:@"SwitchCell"]; 

    if(aCell == nil) 
     { 
     aCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"]; 
     aCell.textLabel.text = [arr objectAtIndex:indexPath.row]; 
     myswitch = [[UISwitch alloc]initWithFrame:CGRectZero]; 
     aCell.accessoryView = myswitch; 
     [myswitch setOn:YES animated:YES]; 

     } 
    switch (indexPath.row) 

    { 
     case 0: 
     { 
      [myswitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 

     } 
      break; 
     case 1: 
     { 
      [myswitch addTarget:self action:@selector(switchChanged1:) forControlEvents:UIControlEventValueChanged]; 
     } 
      break; 

    } 
    return aCell; 
} 

- (void) switchChanged:(id)sender { 
    UISwitch *aswitch = sender; 

    if (aswitch.on==YES) { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hello" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no1" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil]; 
     [alert show]; 

    } 

} 
- (void) switchChanged1:(id)sender { 
    UISwitch *aswitch1 = sender; 

    if (aswitch1.on==YES) { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"second" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no2" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil]; 
     [alert show]; 

    } 
    } 


- (void)dealloc { 
    [tableview release]; 
    [super dealloc]; 
} 
@end 
+0

Powstrzymaj się od publikowania odpowiedzi tylko kodu. Proszę dodać wyjaśnienie. – Sudipta

+0

Dodanie paczki kodu do posta sprzed roku bez wyjaśnienia nie jest zbyt pomocne. Wyjaśnij innym, że to może pomóc – Will

8

Można pobrać NSIndexPath dla UISwitch, który został zmieniony w tableview. Jest to ten sam pomysł dla każdej kontroli, jak już odpowiedział w tym poście: Detecting which UIButton was pressed in a UITableView

- (void) switchChanged:(id)sender 
{ 
    CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]]; 
    NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint]; 
} 

To będzie pracować dla iOS7 wcześniej użyłem nadawcy Superview [], ale że teraz zwraca UITableViewCellContentView wewnątrz UITableViewCellScrollView.

Powiązane problemy