滚动时如何使uitableview单元格不改变?
Posted
技术标签:
【中文标题】滚动时如何使uitableview单元格不改变?【英文标题】:How can make uitableview cells not change when scrolling? 【发布时间】:2016-11-04 17:18:00 【问题描述】:当我滚动我的UITableView
单元格时出现故障,并且我的单元格开始在其他文本之上显示文本。我知道它是因为我使用可重复使用的细胞。我的问题是我怎样才能仍然使用可重复使用的单元格并在滚动时使UITableView
不出现故障?我不想将每个单元格作为实例保存在我的ScheduleViewController
中,因为这违背了可重复使用单元格的目的
代码
视图控制器:
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()
【问题讨论】:
【参考方案1】:每次呈现单元格时,您都会调用 configCell 和 configDot,因此您一直在添加视图。
您应该只在 ScheduleTableViewCell 的 init 方法中创建内部视图,并且每次只填充它们。
更好的方法是在 InterfaceBuilder 中而不是手动创建 UI,但我不知道您的用例。
【讨论】:
以上是关于滚动时如何使uitableview单元格不改变?的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 单元格不保留数据 --- 如何使它们的数据持久化?