1

在我的应用程序中,我有一个表格,每行显示一个单元格。在 Interface Builder 中,我将一个按钮拖到单元格上,将其设置为 Dark Info 按钮,并将其连接到 IBAction。那工作正常。只是,我希望按钮的行为有所不同,具体取决于按钮单元格所在的表格行。我将如何获得该行索引?

我意识到我可能对对象层次结构缺乏基本的了解,但我希望你们能原谅我

谢谢 Sjakelien

4

4 回答 4

1

您可以维护标签。当您拖放按钮时,检查界面构建,您将看到这些按钮的“标记”属性。为每个按钮分配不同的值(我假设您对不同的行有不同的按钮,如果您对不同的行具有相同的单元格标识符,则此解决方案将不起作用)。当您收到事件检查标签值时。我的工作也有类似的问题,我为每个创建的按钮标签维护 NSArray。

于 2009-10-20T15:26:50.343 回答
1

如果您没有先设置一些数据,这绝对不是一件容易的事。如果可以的话,有一个NSDictionary按钮是键,值是索引路径,当你从-tableView:cellForRowAtIndexPath:. 像这样的东西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    [indexDict setValue:indexPath forKey:theButton];
    return cell;
}



- (void) buttonPressed:(UIButton *)button {
    NSIndexPath *indexPath = [indexDict valueForKey:button];
    ...
}
于 2009-10-20T13:51:06.350 回答
0

另一种方法是在视图中更改 UIButton 的基类,然后使用这个其他类,它基本上扩展了 UIButton 并添加了 NSInteger 行 @property(记住 @synthesize 它)。

然后,您将在单元设置期间设置此属性,您可以在 message 方法中检索此属性

于 2009-10-28T11:52:11.660 回答
0

在您的 tableView 委托和数据源方法(检查文档!)中,您有几种方法,最好的方法是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

在您的实现中删除此方法并说类似

    switch (indexPath.row) {
        case 0:
//set variable or do method based on row 0 (first row)
            break;
        case 1:
//set variable or do method based on row 1 (second row)
            break;
        case 2:
//set variable or do method based on row 2 (third row)
            break;
    }//and so on
}
于 2009-10-20T13:49:02.157 回答