【发布时间】:2019-04-23 04:31:50
【问题描述】:
我有一个 UITableView,每个部分加载 1 个单元格,以提供间距。当点击一个单元格时,我可以让单元格适当地扩展。问题是其他细胞也会膨胀,我无法确定原因。未实现多选。 GIF 将说明我的问题。我的代码如下。
来自视图控制器的代码
func numberOfSections(in tableView: UITableView) -> Int {
return searchTextFieldIsEmpty() ? credentials.count : filteredCredentials.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 15 }
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .clear
return view
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CredCell
cell.delegate = self
if credentials.isEmpty {
return cell
} else {
tableView.backgroundView = nil
isFiltering() ? cell.formatCell(filteredCredentials[indexPath.section]) : cell.formatCell(credentials[indexPath.section])
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CredCell
cell.detailsView.isHidden = !cell.detailsView.isHidden
let borderColor = UIColor(red: 206.0/255.0, green: 214.0/255.0, blue: 234.0/255.0, alpha: 1)
cell.idView.addBorder(side: .bottom, thickness: 2, color: borderColor)
cell.notificationView.addBorder(side: .bottom, thickness: 2, color: borderColor)
cell.frontImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
cell.backImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
cell.notesView.addBorder(side: .bottom, thickness: 2, color: borderColor)
tableView.beginUpdates()
tableView.endUpdates()
tableView.deselectRow(at: indexPath, animated: true)
tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}
提前致谢!
使用解决方案编辑 @贾斯汀的建议是导致问题的原因,细胞状态在被重用时仍然存在。我在自定义单元格中添加了以下内容,问题就解决了。
override func prepareForReuse() {
self.detailsView.isHidden = true
}
【问题讨论】:
标签: swift uitableview