如何在滚动时使uitableview单元格不会改变?

时间:2016-11-04 17:18:00

标签: swift uitableview swift3 ios10

当我滚动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 个答案:

答案 0 :(得分:0)

每当您展示一个单元格时,您都会调用 configCell configDot ,这样您就可以随时添加观看次数。

您应该只在ScheduleTableViewCell的 init 方法中创建内部视图,并且每次只填充它们。

更好的方法是在InterfaceBuilder中创建UI而不是手动创建UI,但我不知道您的用例。