我有一个UITableView,它有两个从数组创建的单元格。一旦用户点击第一个单元格,我想调用一个方法,当用户点击第二个单元格时,我想调出另一个ViewController。
我尝试使用此方法对点击事件作出反应:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.extraMenuItems objectAtIndex:indexPath.row];
NSLog(@"\nCell at Index: %@ clicked.\n", indexPath);
}
这是日志:
Cell at Index: <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} clicked. (First item clicked)
Cell at Index: <NSIndexPath: 0xc000000000008016> {length = 2, path = 0 - 1} clicked. (Second item clicked)
如何确定点击了哪个单元?
答案 0 :(得分:3)
只需检查索引路径以确定点击了哪个单元格:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
NSLog(@"First cell tapped!");
}
else if (indexPath.row == 1)
{
NSLog(@"Second cell tapped!");
}
// Deselect the row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}