后滚动(SWIFT)部分的tableview单元格的内容消失
下面是我的自定义单元格类:后滚动(SWIFT)部分的tableview单元格的内容消失
class AthleteTableViewCell: UITableViewCell {
var myLabel1: UILabel!
var myLabel2: UILabel!
var profile: UIImageView!
var star = StarButton()
var touched = false
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:)")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let gap : CGFloat = 10
let labelHeight: CGFloat = 30
let labelWidth: CGFloat = 150
let lineGap : CGFloat = 5
let label2Y : CGFloat = gap + labelHeight + lineGap
myLabel1 = UILabel()
myLabel1.frame = CGRect(x: gap, y: gap, width: labelWidth, height: labelHeight)
myLabel1.textColor = UIColor.black
contentView.addSubview(myLabel1)
myLabel2 = UILabel()
myLabel2.frame = CGRect(x: gap * 5, y: label2Y, width: labelHeight, height: labelHeight)
myLabel2.textColor = UIColor.white
myLabel2.textAlignment = .center
myLabel2.backgroundColor = UIColor.flatMint()
myLabel2.layer.cornerRadius = 15.0
myLabel2.clipsToBounds = true
contentView.addSubview(myLabel2)
profile = UIImageView()
profile.image = UIImage()
profile.frame = CGRect(x: bounds.width - gap, y: bounds.height/2, width: bounds.height * 1.25, height: bounds.height * 1.25)
profile.layer.cornerRadius = (bounds.height * 1.25)/2
profile.layer.masksToBounds = true
contentView.addSubview(profile)
if (touched != false) {
star.isSelected = true
star.frame = CGRect(x: gap, y: label2Y, width: labelHeight, height: labelHeight)
contentView.addSubview(star)
} else {
star.frame = CGRect(x: gap, y: label2Y, width: labelHeight, height: labelHeight)
contentView.addSubview(star)
star.isEnabled = true
}
}
}
和下面是用于创建我的细胞的方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = AthleteTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "myCell")
cell.myLabel1.text = "\(myArray[indexPath.row])"
cell.myLabel1.textColor = UIColor.white
cell.myLabel2.isHidden = true
cell.profile.image = UIImage(named: cell.myLabel1.text!)
cellArray.append(cell)
if (cell.touched) {
cell.star.isSelected = true
} else {
cell.star.isOpaque = true
}
cell.backgroundColor = UIColor(white: 1, alpha: 0.2)
return cell
}
和下面是选择一个单元格的方法,该单元格将一个动画发出,我希望最终状态保持在tableview单元格中。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
makeSelection(tableView, didSelectRowAt: indexPath)
}
func makeSelection(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! AthleteTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
if(selectionArray.contains(myArray.object(at: indexPath.row))) {
//Popping selection off of the stack and updating all labels
updateLabels(tableView: tableView)
selectionArray.remove(myArray[indexPath.row])
cell.star.isFavorite = false
cell.myLabel2?.isHidden = true
//need to somehow implement all of the rank labels decreasing by one if I deselect someone
cell.touched = false
//updateLabels(tableView: tableView)
} else {
selectionArray.add(myArray[indexPath.row])
cell.touched = true
let rank = selectionArray.count
cell.myLabel2.isHidden = false
cell.myLabel2?.text = "\(rank)"
cell.star.isFavorite = true
}
}
这是细胞看上去什么,当你选择它像一张照片:
这就是向下滚动,使后同一小区的照片拿出来看并然后滚动回:
东西显然已经大错 - 我补充说:“感动“布尔到自定义表格视图单元格类的假设,可能单元格正在重绘,并且它无法知道它是否应该有该星形动画或没有,所以它被认为是零,但该修复似乎并不工作(也许我对某事但实施不正确?)
让我知道如果你有任何线索这里发生了什么!非常感谢!!!
您的代码有各种问题。在您的tableView(_:cellForRowAt:)
方法中,您应该致电dequeueReusableCell(withIdentifier:for:)
。
您不应该尝试从单元中读取数据以加载内容。您应该将状态数据保存到某种数据模型中。一个数组适用于具有单个节的表视图,并且一个数组数组适用于分段表视图。
关于如何使用表格视图有无数的教程。你需要去做一些阅读。
我的tableview确实有一个部分。因此阵列。 –
好的,但你的'cellForRow(at:)'方法不检查你选择的单元格数组是否应该设置单元格是否被选中。除了不调用'dequeueReusableCell(withIdentifier:for:)'。 –
提起它作为一个答案这里作为一个循序渐进的过程
1)创建结构为数据
struct Profile {
var personName: String?
var photoURL: String?
var rank: String?
var isTouched: Bool?
// add init method and stuff
}
2)使用这些资料元件填充的阵列。所以最后你有一组配置文件。
3)使用这个数组填充你的tableview,每个单元元素从Profile对象获取数据。
4)在cellForRowAt方法使用dequeueReusableCell(withIdentifier:为:)初始化你的细胞,并赋值
5)如果针对特定的个人资料,isTouched为真,则显示
cell.star.isFavorite = true
其他将其设置为false
6)在您的didSelectRow中,获取indexpath。行,然后在您的配置文件数组中根据您对该特定行的需求设置Profile.isTouched = true/false并相应切换。用新配置文件对象更新您的配置文件数组
7)刷新tableview。
这看起来像经典的细胞回收缺陷。 “UITableView”在屏幕外不会保留单元格,而是将它们循环使用以供重新使用。您无法在单元格中存储实际模型数据。 – marko
理想情况下,您应该将触摸的部分存储在数据存储中,而不是存储在您的单元格中。您的手机不用于存储任何信息。因此,每次单击单元格时,都需要更新数据存储区,以查找特定项目,然后重新加载tableview。 –
@kapsym当你说“更新你的数据存储的特定项目”你是什么意思? –