27

我遇到了 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);
}
4

7 回答 7

34

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

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

解决方案: didSelectRowAtIndexPath

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

完整代码:

// right
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}
于 2013-11-02T00:55:25.030 回答
1

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}
于 2013-01-25T13:05:35.537 回答
1

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

这是由于您实施 DEselect

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

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

解决方案:确保您注意并使用didSelectRowAtIndexPath

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

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

于 2013-09-16T18:12:25.780 回答
0

请将 Xib 的代表连接到文件所有者。然后尝试它会工作。

于 2012-12-27T06:16:49.723 回答
0

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

[self.tableView reloadData]
于 2013-02-18T12:26:24.513 回答
0

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

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

        performSegue(withIdentifier: "Detail", sender: self)
}
于 2016-10-31T21:21:25.403 回答
0

我在情节提要内部的视图上有一个 UIGestureRecognizer。我不得不删除它,它像往常一样工作。

于 2016-05-02T12:37:43.353 回答