2013-05-02 62 views
0

我有一个UIButton定制UITableViewCell(其中动态添加单元格),它定义在与MainViewControllerCustomCell类不同的类上。如果选择了UIButton,我需要MainViewController中的方法返回一个数组。这个数组应该在第n个索引中有关于第n个单元格的信息。 我所写的是:iOS:从UITableViewCell中创建的按钮检索选择状态

CustomCell.h

@interface CustomCell : UITableViewCell 
@property (strong, nonatomic) IBOutlet UIButton *tickButton; 

CustomCell.m

- (IBAction)tick:(UIButton *)sender { 

    if ([sender isSelected]) { 
     [sender setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal]; 
     [sender setSelected:NO]; 
    } else { 
     [sender setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected]; 
     [sender setSelected:YES]; 
    } 
} 

MainViewController.m

-(NSMutableArray*) returnTickArray{ 
    NSMutableArray *tickArray = [[NSMutableArray alloc] initWithCapacity:n]; 
    for (NSInteger i=0; i<n; i++){ 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0]; 
    CustomCell *cell = (CustomCell *) [self.itemTable cellForRowAtIndexPath:indexPath]; 
     if ([cell.tickButton isSelected]){ 
      a=[NSNumber numberWithInt:1]; 
     } 
     else { 
      a=[NSNumber numberWithInt:0]; 
     }; 
     [tickArray addObject:a]; 
    } 

    return tickArray; 

} 

我也尝试过的,而不是使用选择状态的按钮创建一个酒吧lic NSNumber财产并让UIButton使其在10之间交替,然后在MainViewController上“询问”,但它也没有奏效。

回答

1

您正在创建[NSIndexPath indexPathForItem:1 inSection:0];那总是1排。你应该把它替换成'i',就像你在循环中使用的一样。

+0

非常感谢!我不能相信我错过了这一点。 – dietbacon 2013-05-02 23:49:11

相关问题