ios - 如何通过 UIButton 传递参数

标签 ios objective-c uibutton

我必须上课: 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/

相关文章:

iphone - 应用程序仅通过 WiFi 加载数据

ios - 如何重写此类函数?

objective-c - 在 UITableView 中的 UIImageView 中从网络加载图像

objective-c - Objective C 中的内存管理问题

iphone - 进入后台时关闭应用程序

iphone - 我们可以向 iphone native 地址簿 View Controller 添加额外的按钮吗?

ios - 使用 CABasicAnimation 移动时无法使用 UIButton

ios - AVQueuePlayer 第一次点击按钮时只播放一次我的声音序列

ios - 自定义导航栏后退按钮点击区域

ios - 如何快速修复 "Cannot subscript a value of type ' ClosedRange<Int >' with an index of type ' Int'