首页 > 解决方案 > 更新表视图后不起作用。为什么会这样?

问题描述

我的机器上有一个有趣的问题。我想制作表格视图,我做了很多次,但更新后它不起作用。是制作方式改变还是什么?谁能帮我?

这是我的控制器和委托方法:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let tableView: UITableView = {
    let tv = UITableView()
    tv.separatorStyle = .none
    tv.allowsSelection = false
    return tv
}()

override func viewDidLoad() {
    super.viewDidLoad()
    titleOfapp ()
    setupTableView()

}

let sipCellId = "sipCellId"

func titleOfapp () {
    let titleLabelMarka = UILabel()
    titleLabelMarka.textAlignment = .center
    titleLabelMarka.text = "Pak Terminal"
    titleLabelMarka.textColor = UIColor.white
    titleLabelMarka.font = UIFont(name:"Comfortaa-bold", size: 24)
    navigationItem.titleView = titleLabelMarka
    navigationController?.navigationBar.barTintColor = UIColor.init(red: 80.0/255.0, green: 197.0/255.0, blue: 247.0/255.0, alpha: 1.0)
}

func setupTableView() {
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(sipCell.self, forCellReuseIdentifier: sipCellId)
    view.addSubview(tableView)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: sipCellId, for: indexPath) as! sipCell
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
}



}

这里是我的自定义单元格代码:

class sipCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setup()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


let cellView: UIView = {
    let view = UIView()
    view.backgroundColor = .red
    return view
}()

var detailLabel: UILabel = {
    let tf = UILabel()
    tf.text = "mehmet akyol"
    tf.textColor = .black
    tf.font = UIFont(name: "Comfortaa-Bold", size: 13)
    return tf
}()

func setup(){
    addSubview(cellView)
    addSubview(detailLabel)
    cellView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: 60, height: 55)
    detailLabel.anchor(top: nil, left: cellView.leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: 60, height: 40)
    detailLabel.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
}

}

标签: iosswiftxcodeuitableview

解决方案


您需要为表创建约束

view.addSubview(tableView) 
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
    tableView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
    tableView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
    tableView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor) 
])

或设置框架


推荐阅读