【问题标题】:How to save the selected indexpaths of an UICollectionViewCell which is inside a UITableViewCell to an array?如何将 UITableViewCell 内的 UICollectionViewCell 的选定索引路径保存到数组中?
【发布时间】:2017-07-08 04:45:26
【问题描述】:

我在 UITableView 中有 UICollectionViewCells。我正在尝试为 UITableView 的每一行突出显示选定的 UICollectionViewCell。当 UITableView 重新加载选定的单元格时,应突出显示。这是示例代码:

var selectedIndexes = [IndexPath]()
var familyAModelArray : [[Child1]] = []
var childAModelArray : [Child1] = []

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return familyAModelArray[collectionView.tag].count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ChildCollectionViewCell

        cell.nameLabel.text = childAModelArray[indexPath.row].person! // or childArray

        cell.profilePicture.sd_setImage(with: URL(string: "\(baseUrl)\(childAModelArray[indexPath.row].image!)"), placeholderImage: #imageLiteral(resourceName: "avatar.png"), options: [.continueInBackground,.progressiveDownload])

     return cell
        }

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        selectedIndexes.append(indexPath)

        let i = selectedIndexes[collectionView.tag][indexPath.row]

}

这里我的索引超出范围:let i = selectedIndexes[collectionView.tag][indexPath.row]

如何做到这一点?有什么想法吗?提前致谢。

【问题讨论】:

  • 不要将索引路径保存在额外的数组中,而是向数据模型添加一个属性以指示selected 状态。
  • @vadian 感谢您的回复。如果我向数据模型添加一个属性以将选定状态指示为 bool,我如何从 didSelectItemAt indexPath 函数访问它?

标签: arrays swift uitableview uicollectionview didselectrowatindexpath


【解决方案1】:

检查单元格是否被选中的一个好方法是更改​​单元格的背景颜色

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItemAt(indexPath)
    cell.backgroundColor = .red

    // append the selected red cells to an array like such
    selectedIndices.append(indexPath)


}

一旦 tableView 重新加载,您可以检查您的数组中的选定索引并更改背景颜色。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ChildCollectionViewCell

    if selectedIndices.contains(indexPath) {
        cell.backgroundColor = .red
        // check if the index matches a selectedIndex and change it to the selected color.
    }
    cell.nameLabel.text = childAModelArray[indexPath.row].person! // or childArray

    cell.profilePicture.sd_setImage(with: URL(string: "\(baseUrl)\(childAModelArray[indexPath.row].image!)"), placeholderImage: #imageLiteral(resourceName: "avatar.png"), options: [.continueInBackground,.progressiveDownload])

 return cell
}

现在一旦重新加载,将再次调用 cellForItemAt 函数,所选索引路径处的索引将再次变为红色。

【讨论】:

  • 它不应该影响您判断哪个单元格被点击的方式。只需使用表格视图的 didSelectRow 函数检查选择了哪个 tableView 行。
猜你喜欢
  • 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
相关资源
最近更新 更多