单元格在视图上显示不同的文本并选择方法

时间:2015-10-16 04:02:22

标签: ios objective-c uitableview

我有一个包含4个部分的tableview。

    tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         UITableViewCell *cell = [[UITableViewCell alloc]init];
         cell.textLabel.text = [tableData objectAtIndex:indexPath.section]; 
    }

显示正确的数据。但是当选择单元格时,即使在视图中显示正确的数据,文本标签也会显示错误的数据。

tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell.textLabel.text);

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您的问题是您实施了错误的方法。实施didSelectRowAtIndexPath而不是didDeselectRowAtIndexPath

另外,不要从单元格中获取数据。从数据源获取它。你想要:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSLog(@"%@", [tableData objectAtIndex:indexPath.section]);
}