单击时未调用UITableView didSelectRowatIndexPath

时间:2013-11-22 15:58:54

标签: ios uitableview

非常奇怪!它适用于所有地方,但在这里:

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

{
 MyViewController* cliente = [[MyViewController alloc] initWithModeAndClientId:2 c:cid];

            cliente.delegate                    = self;
            UINavigationController *n = [[UINavigationController alloc] initWithRootViewController:cliente];

            n.navigationBarHidden     = NO;
            [[n navigationBar] setBarStyle:UIBarStyleBlack];
            [self presentViewController:n animated:YES completion:nil];
}

如果我点击该行点击,MyViewController会在几秒钟后显示! 如果我点击两次,它会迅速显示! 在Profiler中,单击即可发生任何事情...... 我没有didDeselectRowAtIndexPath方法。

5 个答案:

答案 0 :(得分:6)

解决方案是在主线程上加载第二个控制器

 dispatch_async(dispatch_get_main_queue(), ^{ 
       // Code here 
 });

答案 1 :(得分:3)

它的线程问题,当您在tableview中点击一行时,它会启动一个新线程,因此呈现视图可能需要更长时间才能显示在屏幕上。

解决方案是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    dispatch_async(dispatch_get_main_queue(), ^{

        MyViewController* cliente = [[MyViewController alloc] initWithModeAndClientId:2 c:cid];

        cliente.delegate = self;
        UINavigationController *n = [[UINavigationController alloc] initWithRootViewController:cliente];

        n.navigationBarHidden = NO;
        [[n navigationBar] setBarStyle:UIBarStyleBlack];
        [self presentViewController:n animated:YES completion:nil];
    });
}

答案 2 :(得分:0)

你是否有机会在滚动视图中放置一个tableview?如果是这样,容器scrollview阻止触摸事件到内部表视图。这为我解决了这个问题:

self.myContainerScrollview.panGestureRecognizer.delaysTouchesBegan = true

答案应归功于OP:https://stackoverflow.com/a/31040918/1455770

答案 3 :(得分:0)

有同样的问题。而且很难找到。但这是因为以下原因:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        return nil;
}

您应该返回 indexPath

答案 4 :(得分:0)

我在tableView(swift 4.2)上遇到了同样的问题。经过调试并花费大量时间后,表视图似乎具有名为allowsMultipleSelection的布尔属性。

如果将allowsMultipleSelection设置为true,则表视图选择机制将以以下方式更改:通过选择每个单元格,第一次调用tableView didSelectRowAtIndexPath:并选择同一单元格第二次调用tableView didDeselectRowAtIndexPath:

这使得在同一单元格的第三个选择上调用tableView didSelectRowAtIndexPath:函数,因此结果是双击以调用didSelectRowAtIndexPath:

这意味着,如果一个单元被轻击的次数是奇数(1、3、5 ...),那么将总是调用tableView didSelectRowAtIndexPath:,如果一个单元被轻击的次数是偶数(2 ,4,6,...),则始终会调用tableView didDeselectRowAtIndexPath:

如果您希望在单元格的每个选择上调用tableView didSelectRowAtIndexPath:,则必须设置tableView 多项选择falsetableView.allowsMultipleSelection = false。 / p>

这样做,每次在表视图上点击单元格tableView didSelectRowAtIndexPath:时,都会通过选择另一个单元格tableView didDeselectRowAtIndexPath:来调用之前选择的单元格然后将为新选择的单元格调用tableView didSelectRowAtIndexPath:

    class TableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.allowsMultipleSelection = false
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("This will be called for each cell tap")
    }
相关问题