【问题标题】:Button inside UITableCell - passing data to selectorUITableCell 内的按钮 - 将数据传递给选择器
【发布时间】:2013-10-30 10:34:10
【问题描述】:

这个话题在 stackoverflow 上到处都是,但我找不到合适的解决方案。

我在每个UITableViewCell 上都有按钮。

这是我创建单元格的方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminationCell"];
    UIButton *clipButton = (UIButton *)[cell viewWithTag:3];

    MedicalExamination *examination = [objects objectAtIndex:indexPath.row];

    [clipButton addTarget:self action:@selector(examinatonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

这是处理按钮单击的方法:

-(void)examinatonClicked:(MedicalExamination*)examination
{

}

如何将检查对象传递给examinatonCommentsClicked 方法?显然每个单元格的对象都不同...

【问题讨论】:

标签: ios uitableview selector


【解决方案1】:

我要做的一件有点棘手的事情是,我将使用 UIButton 的 tag 属性来传递行号,这样我就可以从支持我的表的数组中拉出对象。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminationCell"];
    UIButton *clipButton = (UIButton *)[cell viewWithTag:3];
    clipButton.tag = indexPath.row //Set the UIButton's tag to the row.

    MedicalExamination *examination = [objects objectAtIndex:indexPath.row];

    [clipButton addTarget:self action:@selector(examinatonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

-(void)examinatonClicked: (id)sender
{
     int row = ((UIButton *)sender).tag;
     MedicalExamination *examination = [objects objectAtIndex: row];
     //Profit!
}

【讨论】:

    【解决方案2】:

    我认为您必须通过xib(custum cell)uitableviewCell 添加按钮,然后通过iboutlet 连接它。

    【讨论】:

    • 嗯,Dheerendra 有点正确,可以通过这种方式实现......但是我认为 xib 不是必需的,只需自定义 UITableVIewClass
    • 是否可以通过全局对象的NSsstring通过考试?
    • 或创建自定义 uitableviewcell 并使用名为检查的新属性。仅此而已,不是xibs。
    猜你喜欢
    • 2013-11-17
    • 2012-08-31
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多