2012-10-29 103 views
5

我对它们有UITableViewUISwitchs当点击UISwitch时选择UITableView的行

TableView

当开关处于我想运行的功能。该功能只记录开关是打开还是关闭,以及开关已打开的行。 im的问题在于,当我点击开关时,它不会记录正确的行,除非在点击开关之前点击了该行。

我想我的问题是,单击开关不会选择该行。我怎样才能做到这一点,以便它选择行或我可以将ID添加到交换机?

所以开关ID“1”是“ON”。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"POICell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


    //set the cell text to the catName 
    cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]]; 
    //add switch 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; 
    cell.accessoryView = switchView; 
    [switchView setOn:YES animated:NO]; 

    [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
    // Configure the cell... 

    return cell; 
} 

    - (void) switchChanged:(id)sender { 
      NSString *StrCatID =[[NSString alloc]init]; 
      StrCatID = [self.catIDs objectAtIndex:[self.inputTableView indexPathForSelectedRow].row]; 
      UISwitch* switchControl = sender; 
      NSLog(@"The switch for item %@ is %@",StrCatID, switchControl.on ? @"ON" : @"OFF"); 
     } 

回答

10

问题要找到保存开关

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

要找出细胞

NSIndexPath * indexpath = [myTableView indexPathForCell:cell] 

的indexpath在细胞中的案例

- (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

在iOS7中,superview将是一个UITableViewCellContentView,它被包装在UITableViewCellScrollView中。您可以通过使用以下代码获取单元的索引路径:CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]]; NSIndexPath * indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint]; – NSDestr0yer

1

我认为问题在于您使用dequeueReusableCellWithIdentifier,并且您的所有单元格都具有相同的ID。

+0

如果我使用[self.inputTableView indexPathForSelectedRow] .row ID给我选择的行。 – Will

1

一个更好的办法来做到这一点是确定发件人是哪个小区。

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

一旦有了这种方法。那么它的替代[self.inputTableView indexPathForSelectedRow]

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

应设置IndexPath.row作为标签给每个SwitchcellForRowAtIndexPath方法

switchView.tag= indexPath.row; 

;而当开关值变化],即可获得该行号

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

}

+0

我认为这是迄今为止最好的方法。查看视图层次结构的其他方法甚至更糟糕,将点转化为视图,感到骇人听起来像很多工作。这个解决方案可能遇到的唯一问题是,如果您有多个部分,但可以通过使标记对部分和行进行编码来解决。像这样:switchView.tag = indexPath.section * 1000 + indexPath.row; –

4

我不得不做双倍mySwitch.superview.superview才能获得合适的细胞。

下面是一个例子

- (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

避免发布仅有代码的答案。请添加一个解释。 – Sudipta

+0

在没有解释的情况下将一堆代码添加到一年前的帖子中并不是很有用。请解释,所以其他人看到这可能会有所帮助 – Will

8

您可以检索NSIndexPath因为这是在实现代码如下改变UISwitch。这是任何控制相同的思路已经在这个岗位回答: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]; 
} 

这将为iOS7工作,以前我用[发件人上海华],但现在返回UITableViewCellScrollView内部的UITableViewCellContentView。