我有UIButton
一个自定义UITableViewCell
(动态添加单元格),它定义在与该类不同MainViewController
的CustomCell
类上。如果选择了 s,我需要一个方法MainViewController
来返回一个数组。UIButton
该数组应在第 n 个索引中包含有关第 n 个单元格的信息。我写的是:
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;
}
我也尝试过而不是使用按钮选择状态来创建公共NSNumber
属性并UIButton
让它在 and 之间交替1
,0
然后在 上“请求它” MainViewController
,但它也没有奏效。