ios - 多个UITableViewCells在点击时会做不同的事情

标签 ios objective-c uitableview automatic-ref-counting

我有一个包含5个单元格的UITableView。轻按每个按钮时,它们会执行不同的操作。但是现在(为了使代码不要太长),在点击时,它们都调用NSLog

这是代码。

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

    UITableViewCell *newCell = [_randomCells cellForRowAtIndexPath:indexPath];

    if ([(newCell) isEqualToString:@"Random cell 1"]) {
        NSLog(@"foo bar 1.");
    } else if ([(newCell) isEqualToString:@"Random cell 2"]) {
        NSLog(@"foo bar 2.");
    } else if ([(newCell) isEqualToString:@"Random cell 3"]) {
        NSLog(@"foo bar 3.");
    } else if ([(newCell) isEqualToString:@"Random cell 4"]) {
        NSLog(@"foo bar 4.");
    } else if ([(newCell) isEqualToString:@"Random cell 5"]) {
        NSLog(@"foo bar 5.");
    }
}

我认为这将是用Storyboard检测被轻击的UITableViewCell的最合适方法,因为我找不到另一种方法。

但是,在所有五个if语句中,我收到以下错误消息:
No visible @interface for 'UITableViewCell' declares the selector 'isEqualToString:'

我该如何解决这个问题?提前致谢。

请注意,我正在使用Objective-C ARC键入代码,并使用了Storyboards。

最佳答案

看起来您正在检查显示在单元格中的文本?如果是这样,则需要textLabel属性。

尝试这个:

[newCell.textLabel.text isEqualToString:@"Random cell 1"]

祝好运!

关于ios - 多个UITableViewCells在点击时会做不同的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807823/

相关文章:

ios - CoreData 在插入和删除同时发生时崩溃

iphone - 从其他 NSThread 委托(delegate)回调

ios - 重新加载数据未调用 cellForRowAtIndexPath :

ios - NSQueueOperation 奇怪的行为

ios - 多次使用 textViewShouldBeginEditing

ios - 如何维护 NSMutableDictionary 中添加的键的顺序

ios - 在 UITableView 中拖放整个部分而不是单行

ios - 如何在 Objective C 中的自定义 TableView UILabel 上设置数据模型值

ios - 在 UITableView 编辑模式下更改删除按钮标题

iphone - UITableView 自定义单元格抛出 SIGABRT 错误