如何防止第二次单击UITableView
我的应用程序在tableView:didSelectRowAtIndexPath中调用一个块,并在块中显示一个视图控制器。 如果我在第一次单击正在进行时第二次单击该单元格,则会崩溃。 如何防止第二次单击该单元格?
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath [dataController fetchAlbum:item success:^(Album *album) { ... ... [self presentViewController:photoViewController animated:YES completion:nil]; }];
在didSelectRow
的开头,关闭桌面上的用户交互。
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { tableView.userInteractionEnabled = NO; ...
您可能希望稍后在fetchAlbum
完成时将其重新打开(在主线程上执行此操作),以便在用户返回此视图(或提取失败)时,他们可以再次与表进行交互。
对于swift 3:
当用户选择一行时,请关闭用户互动:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.isUserInteractionEnabled = false
每当视图出现时,不要忘记将其打开:
override func viewDidAppear(_ animated: Bool) { tableView.isUserInteractionEnabled = true }
你可以阻止多次点击(通过禁用表格或用微调器覆盖它),或者你可以让didSelectRowAtIndexPath
同步显示你的视图控制器并在它出现后加载你的“相册”。 我是后者的粉丝,因为它让UI感觉更敏感。
您想禁用单元格上的用户交互:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; cell.userInteractionEnabled = NO;
正如Stonz2指出的那样,你可能想要对整个tableview做这件事,而不是特定的单元格,如果你正在展示一个VC。
let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.userInteractionEnabled = false
然后在导航到另一个视图之前设置回true,否则当您返回到表时,单元格仍将被禁用。