我有一个包含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);
可能是什么问题?
答案 0 :(得分:0)
您的问题是您实施了错误的方法。实施didSelectRowAtIndexPath
而不是didDeselectRowAtIndexPath
。
另外,不要从单元格中获取数据。从数据源获取它。你想要:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSLog(@"%@", [tableData objectAtIndex:indexPath.section]);
}