我必须上课: existUserView 和 existUserCustomCell。
existUserView中的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"];
KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row];
cell.kidName.text = kid.firstName;
if([kid.inside isEqualToString:@"1"]){
cell.kidStatus. text = @"some string";
}else{
cell.kidStatus.text = @"some string";
}
return cell;
}
existUserCustomCell 中的代码:
- (IBAction)reportMissing:(id)sender {
}
- (IBAction)callTeacher:(id)sender {
}
我需要将数据从“existUserView”传递到“existUserCustomCell”中的两个按钮功能,并在我按下它们时知道行。 我怎样才能做到最好?
最佳答案
如果您需要将数据从 View Controller 传递到单元格,请向单元格类添加一个(或两个或三个)属性。在 cellForRowAtIndexPath
中设置单元格时设置该属性。然后您的单元格方法(包括按钮处理程序)可以根据需要访问数据。
将属性添加到您的单元格类:
@interface ExistUserCustomCell : UITableViewCell
// add this to anything you have
@property (nonatomic, strong) KindManager *kid;
@end
现在您的按钮方法可以访问:
- (IBAction)reportMissing:(id)sender {
// access self.kid to get data
// anything else you need
}
然后在 TableView Controller 中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"];
KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row];
cell.kid = kid;
cell.kidName.text = kid.firstName;
if([kid.inside isEqualToString:@"1"]){
cell.kidStatus. text = @"some string";
}else{
cell.kidStatus.text = @"some string";
}
return cell;
}
我猜这是您在单元格中需要的 KidManager
数据。根据需要进行调整。
顺便说一句 - 如果我的猜测是正确的,那么单元格应该使用数据自行设置,而不是在 cellForRowAtIndexPath
方法中使用逻辑。
关于ios - 如何通过 UIButton 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846126/