UITableView
存在一个奇怪的问题。我有 3 个数据源,这意味着 UITableView 中有 3 个部分。当我滚动 UITableView
时,按钮和图像发生冲突。按钮消失,图像变形。
đây là của tôicellForRowAt
phương pháp.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventnetworkingcell", for: indexPath) as! EventNetworkCell
var name: String = ""
var job: String = ""
var company: String = ""
var img: Int?
cell.userImageView.layer.cornerRadius = 45
cell.userImageView.clipsToBounds = true
if indexPath.section == 0 {
name = self.matchmaking[indexPath.row].name
job = self.matchmaking[indexPath.row].job
company = self.matchmaking[indexPath.row].company
img = self.matchmaking[indexPath.row].image
}
else if indexPath.section == 1 {
name = self.networkInEvent[indexPath.row].name
job = self.networkInEvent[indexPath.row].job
company = self.networkInEvent[indexPath.row].company
img = self.networkInEvent[indexPath.row].image
cell.addButtonOutlet.alpha = 0
}
khác {
name = self.allAttendees[indexPath.row].name
job = self.allAttendees[indexPath.row].job
company = self.allAttendees[indexPath.row].company
img = self.allAttendees[indexPath.row].image
}
cell.nameLabel.text = name
cell.jobLabel.text = job
cell.companyLabel.text = company
if let imgid = img {
let url = MTApi.url(for: imgid, size: .normal)
cell.userImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
}
}
cell.addButtonOutlet.addTarget(self, action:
#selector(self.addNetwork(sender:)), for: .touchUpInside)
return cell
}
当我删除 cell.addButtonOutlet.alpha = 0
行时,按钮不会消失。
并且有一个视频显示了该问题:
Video
您在重复使用单元格时遇到问题。
基本上,发生的情况是,tableView 仅创建与屏幕上可见的单元格一样多的单元格,一旦单元格滚动到 View 之外,它就会被数据源中的另一个项目重用。
按钮看似消失的原因是您之前已经删除了该按钮,但现在重新使用时没有告诉单元格再次显示该按钮。
解决这个问题很简单,只需添加:
cell.addButtonOutlet.alpha = 0
到您的第 0 和 2 部分(否则 block )。
与图像相同,除非您告诉单元格在需要时删除图像,否则之前的图像将被保留,因此只需添加以下内容:
if let imgid = img {
let url = MTApi.url(for: imgid, size: .normal)
cell.userImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
} khác {
cell.userImageView.image = nil
}
Tôi là một lập trình viên xuất sắc, rất giỏi!