【问题标题】:Convert Multiple Selection DidSelectRowAtIndexPath from Objective C to Swift 3将多选 DidSelectRowAtIndexPath 从 Objective C 转换为 Swift 3
【发布时间】:2017-05-28 02:54:27
【问题描述】:

我正在将我的应用从 Objective C 转换为 Swift。除了这个例外,我在所有领域都做得很好。在我的目标 c 文件中,我有一个允许多项选择的 UITableView。当用户选择一个单元格时,来自该对象的信息将存储在一个数组中。当用户再次单击单元格时,该对象将被删除。我试图弄清楚这在 Swift 3 中是如何工作的,我可以添加对象,但我似乎无法弄清楚如何从数组中删除该对象。请指教。下面是我正在尝试转换的来自 Objective C 的代码。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        RibbonsInfo *ribbonsInfo = [ribbonsArray objectAtIndex:indexPath.row];
        UITableViewCell *cell = [ribbonTableView cellForRowAtIndexPath:indexPath];
        if (ribbonTableView.allowsMultipleSelection == YES) {
            if(cell.accessoryType == UITableViewCellAccessoryNone) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                [selectedRibbons addObject:ribbonsInfo];

            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;
                [selectedRibbons removeObject:ribbonsInfo];
            }
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }

【问题讨论】:

    标签: objective-c swift uitableview didselectrowatindexpath


    【解决方案1】:

    使用 Swift,您只能使用 indexArray 中删除项目。所以你需要在那个数组中获取那个对象的索引,然后调用selectedRibbons.remove(at: index)

    例如。

    var array = Array<String>()
    
    array.append("apple")
    
    array.append("banana")
    
    array.append("orange")
    
    print(array) // ["apple", "banana", "orange"]
    
    if let index = array.index(of: "banana") {
    
        array.remove(at: index)
    }
    
    print(array) // ["apple", "orange"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多