【发布时间】:2019-12-31 13:05:44
【问题描述】:
My tableview has a weird behavior when a cell is selected but this behavior is not seen always.当一个单元格被选中时,所选单元格下方的一些单元格会移动。这两个 gif 将在显示和不显示时向您展示两种情况下的行为。 这是 tableview without weird behavior 这是tableview with the weird behavior 这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UserTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "userCell") as? UserTableViewCell)!
if(self.firebaseUsers.count != 0){
cell.influentBackgroudView.layer.cornerRadius=10
let url = URL.init(string: String().loadCorrectUrl(url:firebaseUsers[indexPath.row].image))
cell.influentImageView.contentMode = .scaleAspectFit
cell.influentImageView!.sd_setImage(with: url!, placeholderImage: UIImage.init(named: "placeholder"),options: [.continueInBackground,.progressiveDownload], completed: { (image, error, cacheType, imageURL) in
if (error != nil) {
cell.influentImageView!.image = UIImage(named: "placeholder")
} else {
cell.influentImageView.contentMode = .scaleAspectFill
cell.influentImageView.image = image
}
})
cell.influentImageView.layer.cornerRadius=10
cell.influentImageView.layer.masksToBounds = true
cell.influentNameLabel.text=" " + firebaseUsers[indexPath.row].name + " "
cell.influentNameLabel.adjustsFontSizeToFitWidth = true
cell.influentNameLabel.textAlignment = .center
if(selectedCellIndex==indexPath ){
cell.influentBackgroudView.isHidden=false
}
else{
cell.influentBackgroudView.isHidden=true
}
cell.selectionStyle = .none
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let previousIndex=selectedCellIndex
selectedCellIndex=indexPath
if(previousIndex==selectedCellIndex){
let nextVC = storyboard?.instantiateViewController(withIdentifier: "VisitedProfileViewController") as! VisitedProfileViewController
nextVC.passedUser = firebaseUsers[selectedCellIndex!.row]
navigationController?.pushViewController(nextVC, animated: true)
}
if(previousIndex==nil){
tableView.reloadRows(at: [indexPath], with:.none)
}
else{
if(previousIndex != indexPath){
tableView.reloadRows(at: [indexPath,previousIndex!], with: .none)
}
else {
tableView.reloadRows(at: [indexPath], with: .none)
}
}
}
谢谢大家的帮助!
【问题讨论】:
-
可能不需要重新加载任何行,但既然你这样做了,我认为这个问题只会在你向下滚动表格视图时发生。我要说的第一个嫌疑人是行的估计高度与实际单元格高度不一致。如果您有静态单元格高度,请尝试在估计的高度委托方法中返回相同的值。如果您有自动单元格高度,您可以将实际高度存储到字典
[IndexPath: CGFloat]中并在您的委托中使用该高度。你可以填写在didEndDisplaying委托方法(hightCache[indexPath] = cell.bounds.height)。 -
嘿马蒂奇!谢谢您的回答,我只是将行高常量放在估计的行高委托方法中,问题就消失了。你救了我的一天! (将您的评论作为答案,我会接受并投票)
标签: ios swift xcode uitableview cell