【发布时间】: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