【问题标题】:Highjack or prevent UITableView didSelectRowAtIndexPath劫持或阻止 UITableView didSelectRowAtIndexPath
【发布时间】:2012-06-20 16:35:39
【问题描述】:

我正试图找到一种方法来防止或劫持didSelectRowAtIndexPath:。当用户在 tableview 中选择一行时,我想首先抛出一个 alertview 说“如果你这样做,数据将从 CD 中删除。” “如果您继续,我们会将数据同步到服务器”。

用户点击是继续。我想防止在同步完成之前选择新单元格。如果同步失败,我想弹出一个警报,告诉用户它失败了,然后停止didSelectRowAtIndexPath: 触发,从而防止他们触摸的新单元格被选中。

如果同步成功,我想调用didSelectRowAtIndexPath:

最好的方法是劫持willSelectRowAtIndexPath:吗?

【问题讨论】:

    标签: objective-c ios uitableview didselectrowatindexpath


    【解决方案1】:

    实现委托方法

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

    并为相关的 indexPath 返回nil

    这将停止选择相关单元格。

    来自UITableViewDelegate 的文档

    返回值 一个索引路径对象,用于确认或更改选定对象 排。如果需要,返回 indexPath 以外的 NSIndexPath 对象 要选择的另一个单元格。如果您不想要该行,则返回 nil 已选中。

    【讨论】:

    • 这阻止了它,但是我该如何继续选择。我假设只是用他们选择的索引手动调用 didSelectRowAtIndexPath (我会从 willSelectRow 中保留..)?
    • 如果您希望在视觉上选择单元格,您必须保留指向它的指针并调用 [cell setSelected:YES animated:YES];
    【解决方案2】:

    知道了。

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableView *roomTable = tableView;
    Rooms *room = [roomArray objectAtIndex:indexPath.row];
    NSString *message = [NSString stringWithFormat:@"Switching rooms will remove data for current thing. Need to download %@ room", room.name];
    [UIActionSheet actionSheetWithTitle:message 
                                message:@"Message" 
                 destructiveButtonTitle:@"Continue" 
                                buttons:[NSArray arrayWithObjects:nil]
                             showInView:self.view 
                              onDismiss:^(int buttonIndex)
     {
         NSLog(@"User selected to change the room");
         [roomTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
     }
                               onCancel:^
     {
         NSLog(@"Change Room Cancelled");
     }];
    
    return nil;   
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-14
      • 2019-05-08
      • 2012-08-27
      • 2012-04-17
      • 2012-05-19
      • 1970-01-01
      • 2011-12-29
      • 2014-12-22
      相关资源
      最近更新 更多