【问题标题】:How to split indexPath array into seperate array of indexPath, each array's indexPath has same indexPath.section如何将 indexPath 数组拆分为单独的 indexPath 数组,每个数组的 indexPath 具有相同的 indexPath.section
【发布时间】:2019-04-27 04:26:35
【问题描述】:

最近想根据indexPaths删除单元格,所以函数的输入参数是[IndexPath]类型,我需要根据indexPath.section将[IndexPath]拆分成几个数组,有没有简单的方法做这个? 例如

indexPaths = 
[IndexPath(row: 0, section: 1),
 IndexPath(row: 1, section: 1), 
 IndexPath(row: 2, section: 1), 
 IndexPath(row: 2, section: 0)]

想把它转换成

indexPath1 = 
[IndexPath(row: 0, section: 1),
 IndexPath(row: 1, section: 1), 
 IndexPath(row: 2, section: 1)]

indexPath0 = 
[IndexPath(row: 2, section: 0)]

// maybe get a [Array]
[indexPath0, indexPath1]

【问题讨论】:

  • 请不要忘记通过单击最能解决您的问题的答案左侧的复选标记来表明您的问题已成功回答。在你来这里的所有这些年里,你没有对任何问题这样做过。您应该回顾所有问题,并在适当的情况下检查答案是否最能解决该问题。

标签: swift indexpath


【解决方案1】:

一种可能的解决方案是首先构建一个字典,其中键是节号,值是该节中IndexPath 的数组。

let indexPaths = [
    IndexPath(row: 0, section: 1),
    IndexPath(row: 1, section: 1),
    IndexPath(row: 2, section: 1),
    IndexPath(row: 2, section: 0),
]

let pathDict = Dictionary(grouping: indexPaths) { (path) in
    return path.section
}

然后您可以将此字典映射到路径数组的数组中。但首先按部分对这些数组进行排序。

let sectionPaths = pathDict.sorted { (arg0, arg1) -> Bool in
    return arg0.key < arg1.key // sort by section
}.map { $0.value } // get just the arrays of IndexPath

print(sectionPaths)

输出:

[[[0, 2]], [[1, 0], [1, 1], [1, 2]]]

【讨论】:

    【解决方案2】:
    • 我们需要一个HashMap,映射到键上。
    • 我们需要对字典进行排序
    • 我们需要提取字典的值并将它们附加到数组中
    • 我们需要返回那个数组
    var indexPaths = [IndexPath(row: 0, section: 1),
         IndexPath(row: 1, section: 1),
         IndexPath(row: 2, section: 1),
         IndexPath(row: 2, section: 0)
    ]
    
    extension Array where Element == IndexPath {
        func splitArray() ->  Array<[IndexPath]> {
            var tempDict = [String : [IndexPath]]()
            for element in self {
                let section = element.section
                if tempDict[String(section)] != nil {
                    // some element append
                    if var array = tempDict[String(section)] {
                        array.append(element)
                        tempDict[String(section)] = array
                    }
    
                } else {
                    tempDict[String(section)] = [element]
                }
            }
    
            // dictionary mayn't have sorted, sort the dictionary 
    
            tempDict.sorted{ $0.key > $1.key }
    
            var returnedArray = Array<[IndexPath]>()
            for (key, value) in tempDict {
               returnedArray.append(value)
            }
    
            return returnedArray
        }
    }
    
    print(indexPaths.splitArray())
    
    

    【讨论】:

      【解决方案3】:

      使用过滤器:

      let indexPath0 = indexPaths.filter { $0.section == 0 }
      let indexPath1 = indexPaths.filter { $0.section == 1 }
      

      【讨论】:

      • 这无法扩展。如果原来的indexPaths有几十个部分怎么办?您不需要为每个部分创建单独变量的解决方案。
      • 如果没有怎么办?这只是对问题的快速回答,当然它不是最有效和可扩展的。感谢您提及。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多