关于UITableView有一个奇怪的问题。我有3个数据源,这意味着在UITableView中有3个部分。当我滚动UITableView时,按钮和图像发生冲突。按钮正在消失,图像正在变形。
这里是我的cellForRowAt方法。
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
}
else {
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行时,按钮并没有消失。
有一个视频显示了这个问题:
发布于 2019-04-11 16:47:47
你在细胞重用方面遇到了问题。
基本上,实际情况是,tableView创建的单元格与屏幕上可见的单元格一样多,一旦一个单元格从视图中滚动出来,它就会重用于dataSource中的另一个项目。
按钮似乎消失的原因是,您以前已经删除了按钮,但现在,当重用时,没有告诉单元格再次显示按钮。
解决这个问题很简单,只需添加:
cell.addButtonOutlet.alpha = 0添加到您的0和2节(else块)。
图像也是一样,前一个图像会被保留,除非你告诉计算单元在需要的时候删除图像,所以只需添加以下内容:
if let imgid = img {
let url = MTApi.url(for: imgid, size: .normal)
cell.userImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
} else {
cell.userImageView.image = nil
}发布于 2019-04-11 16:48:20
var name: String = ""
var job: String = ""
var company: String = ""
var img: Int?
cell.userImageView.layer.cornerRadius = 45
cell.userImageView.clipsToBounds = true
cell.addButtonOutlet.alpha = 1 // add more
if indexPath.section == 0 {
........你应该研究一下UITableviewCell的重用。
https://stackoverflow.com/questions/55628193
复制相似问题