【问题标题】:Multiple UITableViewCells doing different things when tapped多个 UITableViewCells 在点击时做不同的事情
【发布时间】:2013-09-15 00:42:44
【问题描述】:

我有一个UITableView,有 5 个单元格。当每个被点击时,它们会做不同的事情。但是现在(为了让代码不会太长),当他们点击时,他们都会喊出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 检测被窃听的UITableViewCells 的最合适方法,因为我找不到其他方法。

但是,在所有五个 if 语句中,我都会收到以下错误消息:

No visible @interface for 'UITableViewCell' declares the selector 'isEqualToString:'

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

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

【问题讨论】:

  • 通常,您可以通过使用 indexPath.row 来执行此操作,而不是查看单元格中的文本。

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


【解决方案1】:

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

试试这个:

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

祝你好运!

【讨论】:

  • 它不起作用,只是因为.text没有添加到newCell.textLabel的末尾。它现在正在工作,我仍然会将其标记为正确。再次感谢您。
  • 我进行了编辑以反映这一点,因此代码可用,抱歉疏忽,我是从记忆中开始的 :)
【解决方案2】:

您可以使用 indexpath 检测被点击的 UITableViewCell...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row==0 ) 
        NSLog(@"foo bar 1.");
     else if (indexPath.row==1)
        NSLog(@"foo bar 2.");
    else if (indexPath.row==2)
        NSLog(@"foo bar 3.");
    else if (indexPath.row==3) 
        NSLog(@"foo bar 4.");
    else if (indexPath.row==4) 
        NSLog(@"foo bar 5.");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多