当我滚动 UITableView
单元格时,单元格出现故障,并且我的单元格开始在其他文本之上显示文本。我知道这是因为我使用可重复使用的电池。我的问题是如何仍然使用可重用单元格并使 UITableView
在滚动时不会出现故障?我不想将每个单元格保存为 ScheduleViewController
中的实例,因为这违背了可重用单元格的目的
代码
View Controller :
class ScheduleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var table: UITableView!
private var stops: [String]!
private var times: [String]!
private let identifier: String = "Schedule Cell"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? ScheduleTableViewCell
if cell == nil{
cell = ScheduleTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
}
var anchor: Bool
if stops[indexPath.row] == "Jessup & Program House Drive"{
anchor = true
}else{
anchor = false
}
cell?.configCell(scheduleViewBounds: self.view.bounds, timeData: times[indexPath.row], stopData: stops[indexPath.row])
cell?.configDot(filled: anchor)
return cell!
}
自定义 TableView 单元格:
class ScheduleTableViewCell: UITableViewCell {
private var stop: UILabel!
private var time: UILabel!
private var dot: CAShapeLayer!
private var map: UIButton!
private var line: UIView!
private var seperator: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String!){
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
func configCell(scheduleViewBounds: CGRect, timeData: String, stopData: String){
self.isUserInteractionEnabled = false
let height = self.contentView.bounds.height
line = UIView(frame: CGRect(x: scheduleViewBounds.width * 0.20, y: 0, width: 3.5, height: height))
line.backgroundColor = Color.red
time = UILabel()
time.text = timeData
time.font = UIFont(name: "HelveticaNeue-Light", size: 11.5)
time.textColor = Color.black
time.sizeToFit()
time.center.y = self.contentView.center.y
time.center.x = scheduleViewBounds.width * 0.10
stop = UILabel()
stop.frame = stop.frame.offsetBy(dx: line.frame.maxX+15, dy: 0)
stop.text = stopData
stop.font = UIFont(name: "HelveticaNeue", size: 13.0)
stop.textColor = Color.black
stop.sizeToFit()
stop.center.y = self.contentView.center.y
map = UIButton(frame: CGRect(x: scheduleViewBounds.width - time.frame.minX - 40, y: 0, width: 40, height: 16))
map.setTitle("Map", for: UIControlState.normal)
map.setTitleColor(Color.grey, for: UIControlState.normal)
map.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 11.3)
map.setImage(UIImage(named: "pin"), for: UIControlState.normal)
let spacing = CGFloat(5.0)
let insetAmount = spacing / 2
map.imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
map.titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
map.contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
map.sizeToFit()
map.center.y = contentView.center.y
seperator = UIView(frame: CGRect(x: stop.frame.minX - 5, y: height - 1, width: scheduleViewBounds.width, height: 1))
seperator.backgroundColor = Color.lightgrey
dot = CAShapeLayer()
dot.strokeColor = Color.red.cgColor
let circlePath = UIBezierPath(arcCenter: CGPoint(x: line.center.x,y: self.contentView.center.y), radius: CGFloat(4.0), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
dot.path = circlePath.cgPath
dot.lineWidth = 3.0
self.contentView.addSubview(stop)
self.contentView.addSubview(line)
self.contentView.addSubview(time)
self.contentView.addSubview(map)
self.contentView.addSubview(map)
self.contentView.addSubview(seperator)
self.contentView.layer.addSublayer(dot)
}
func configDot(filled: Bool){
if filled{
dot.fillColor = Color.red.cgColor
stop.font = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
stop.sizeToFit()
time.font = UIFont(name: "HelveticaNeue-Medium", size: 12.0)
time.sizeToFit()
}else{
dot.fillColor = UIColor.white.cgColor
stop.font = UIFont(name: "HelveticaNeue", size: 13.0)
stop.sizeToFit()
time.font = UIFont(name: "HelveticaNeue-Light", size: 11.5)
time.sizeToFit()
}
}
最佳答案
每次呈现单元格时,您都会调用 configCell 和 configDot,以便始终添加 View 。
您应该仅在 ScheduleTableViewCell 的 init 方法中创建内部 View ,并且每次只填充它们。
更好的方法是在 InterfaceBuilder 中创建 UI,而不是手动创建 UI,但我不知道您的用例。
关于swift - 如何让uitableview单元格在滚动时不发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40428398/