多个UITableViewCells在点击时执行不同的操作

时间:2013-09-15 00:42:45

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

我有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检测tapped UITableViewCell的最合适方式,因为我找不到其他方法。

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

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

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

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

2 个答案:

答案 0 :(得分:3)

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

试试这个:

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

祝你好运!

答案 1 :(得分:0)

您可以使用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.");
}