【问题标题】:UITableView didSelectRowAtIndexPath not being called on first tap第一次点击时未调用 UITableView didSelectRowAtIndexPath
【发布时间】:2012-02-24 05:03:42
【问题描述】:

iOS 5 的 UITableView 的 didSelectRowAtIndexPath 存在问题,这在 ios 4 中是正确的。

第一次点击表格中的任何行时,不会调用该方法。一旦我选择了另一行,它就会调用 didSelectRowAtIndexPath,但会传递最后一个 indexPath。

tableView.delegate我已经设置好了,在ios4下可以正常运行。

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);
}

【问题讨论】:

  • 你能发布代码吗?你确定你没有实现 didDeselectRowAtIndexPath 吗?
  • 我没有实现取消选择。谢谢。
  • @AnnaKarenina - 今天早上我遇到了这个问题,Anna Karenina 再次拯救了我的皮肤 - 谢谢 :)
  • 这个问题解决了吗?你能回答(你自己的)这个问题,所以我们可以关闭它!?

标签: uitableview ios5 didselectrowatindexpath


【解决方案1】:

您可能已经实现(注意取消选择):didDeselectRowAtIndexPath

...因此在选择取消选择第一个单元格的新单元格时触发,并将 indexPath 记录为第一个单元格。

解决方案: didSelectRowAtIndexPath

是的,它们看起来超级相似,而且 didDeselectRowAtIndexPath 是 Xcode 的自动完成大多数时间选择的第一个方法也无济于事。

完整代码:

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

【讨论】:

  • 谢谢!想知道为什么我的警报视图显示最后选定行的详细信息!
  • 刚刚来到这里是因为我在 iOS8 Swift 项目中遇到的一个非常相似的问题。这正是我所做的。谢谢!
  • 如果您 bold 选择和取消选择会有所帮助。需要花点时间才能看到它。
  • 谢谢,浪费了 30 分钟;本来可以更多
【解决方案2】:

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

- (void)viewDidLoad
{
    [super viewDidLoad];

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

【讨论】:

    【解决方案3】:

    我不确定这是否已经得到解答,但我正在处理这个问题,就像上面评论的“安娜卡列尼娜”一样。确保您密切关注细节。

    这是由于您实现了 DEselect

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    

    这将导致第一次点击不起作用,但随后的点击和任何其他单元格将按预期工作。

    解决方法:一定要注意并使用didSelectRowAtIndexPath

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    希望有所帮助,就像我之前提到的那样,“安娜卡列尼娜”解决了这个问题

    【讨论】:

      【解决方案4】:

      请将 Xib 的代表连接到文件所有者。那就试试吧。

      【讨论】:

        【解决方案5】:

        在您的 viewDidLoad 方法中添加此代码

        [self.tableView reloadData]
        

        【讨论】:

          【解决方案6】:

          我在故事板内部的视图上有一个 UIGestureRecognizer。我不得不将其删除,它正常工作。

          【讨论】:

            【解决方案7】:

            我在将数据传递给另一个 VC 时也遇到了这个问题。我通过从 Table VC 切换到手动 segue 到 detail VC 来解决它。

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                    selectedData = data[indexPath.row]
            
                    performSegue(withIdentifier: "Detail", sender: self)
            }
            

            【讨论】:

            • 如果确实调用了 didselect 方法,应该如何解决它?请帮助我了解更多谢谢:)
            猜你喜欢
            • 2011-01-07
            • 2015-03-10
            • 2015-01-20
            • 2013-12-07
            • 2012-09-18
            • 1970-01-01
            • 1970-01-01
            • 2011-08-06
            相关资源
            最近更新 更多