UITableView的didSelectRowAtIndexPath在第一次点击时未被调用

27

我在处理iOS 5中的UITableView的didSelectRowAtIndexPath时遇到问题,而在iOS 4中是正确的。

当我首次点击表格中的任意行时,该方法不会被调用。一旦我选择另一行,它就会调用didSelectRowAtIndexPath,并传递最后一个indexPath。

我已经设置了tableView.delegate,并且在iOS 4中可以正常运行。

MainView.xib
UITabBarController
     --UINavigationController
         --**UITableViewController**

有什么建议吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [[NSString alloc]initWithFormat:@"%d",indexPath.row];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",indexPath.row);
}

29
你能否发布这段代码?你确定你没有实现didDeselectRowAtIndexPath吗? - user467105
2
@AnnaKarenina - 今天早上我遇到了这个问题,Anna Karenina 再次帮了我大忙 - 谢谢 :) - bodacious
2
可能是重复的问题:UITableView didSelectRowAtIndexPath: not being called on first tap - Donal Fellows
4
这个问题解决了吗?你能回答(自己的)这个问题吗,这样我们就可以关闭这个问题了! - Roger
视图上的另一个手势识别器可能是原因。 - Sudheesh
显示剩余5条评论
7个回答

34

你可能已经实现了(注意 Deselect):didDeselectRowAtIndexPath

......因此,当选择新单元格并取消选择第一个单元格时会触发,并将indexPath作为第一个单元格记录下来。

解决方案:didSelectRowAtIndexPath

它们看起来非常相似,而且不利的是,didDeselectRowAtIndexPath是Xcode自动完成大多数时间选择的第一个方法。

完整代码:

// right
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}

1
谢谢!我在想为什么我的警告视图会显示上次选择行的详细信息! - Rajeev Bhatia
刚来这里是因为我遇到了一个与我的iOS8 Swift项目非常相似的问题。这正是我所做的。谢谢! - Jase
如果您将“选择”和“取消选择”加粗,会有所帮助。这需要一些盯着看才能发现。 - gjpc
谢谢,浪费了30分钟;不过本来可能会更多。 - Fouad

1
请尝试将您的-viewDidLoad更改为以下代码(插入最后一行)。告诉我结果。
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}

1
我不确定是否已经有人回答了这个问题,但我也遇到了这个问题,就像上面的“安娜·卡列尼娜”评论所说。请确保您仔细注意细节。
这是由于您实施了DEselect引起的。
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

这将导致第一次点击无效,但随后的点击和任何其他单元格都将按预期工作。
解决方案:确保注意并使用didSelectRowAtIndexPath。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

希望这能有所帮助,就像我之前提到的,这个问题已经被“安娜·卡列尼娜”解决了。

0

当我将数据传递到另一个视图控制器时,也遇到了这个问题。我通过从表视图控制器切换到手动跳转到详细视图控制器来解决了它。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedData = data[indexPath.row]

        performSegue(withIdentifier: "Detail", sender: self)
}

如果didselect方法被调用了,那么它应该如何解决这个问题?请帮我更好地理解,谢谢:) - ahmedHegazi

0
请将Xib中的委托连接到文件所有者。然后尝试,它将正常工作。

0

我在Storyboard里的视图上添加了一个UIGestureRecognizer。但是我不得不将其删除,才能让程序正常运行。


0
将以下代码添加到您的viewDidLoad方法中。
[self.tableView reloadData]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接