Arrays 如何将UITableViewCell中的UICollectionViewCell的选定索引路径保存到数组中?

Arrays 如何将UITableViewCell中的UICollectionViewCell的选定索引路径保存到数组中?,arrays,swift,uitableview,uicollectionview,didselectrowatindexpath,Arrays,Swift,Uitableview,Uicollectionview,Didselectrowatindexpath,UITableView中有UICollectionViewCells。我试图为UITableView的每一行高亮显示选定的UICollectionViewCell。当UITableView重新加载时,所选单元格应高亮显示。以下是示例代码: var selectedIndexes = [IndexPath]() var familyAModelArray : [[Child1]] = [] var childAModelArray : [Child1] = [] func collectionV

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=selectedIndex[collectionView.tag][indexath.row]


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

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

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函数,选定索引路径的索引将再次变为红色。

不要将索引路径保存在额外的数组中,而是向数据模型添加一个属性,以指示选定的状态。@vadian感谢您的回复。如果我向数据模型添加一个属性,将所选状态指示为bool,那么如何从didSelectItemAt indexPath函数访问它?它不应该影响您判断哪个单元格被点击的方式。只需使用表视图的didSelectRow函数检查选择了哪个表视图行。