ios - 更新 tableview 后不起作用。为什么会这样?

标签 ios swift xcode uitableview

我的机器有一个有趣的问题。我想制作表格 View 并且我做了很多次但是更新后它不起作用。是制作方式变了还是怎么的?谁能帮我?

这里是我的 Controller 和委托(delegate)方法:

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
}

}

最佳答案

您需要为表创建约束

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) 
])

或设置一个框架

关于ios - 更新 tableview 后不起作用。为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58914935/

相关文章:

ios - UIImageJPEGRepresentation Swift 的错误循环

ios - 如何以编程方式打开WhatsApp,然后返回我的应用程序?

ios - 如何在iOS应用程序中将小数位限制为四位

ios - Xcode 5 不打印异常详细信息

ios - 有没有办法在主 iOS 应用程序中打开或关闭 Sticker Pack 应用程序扩展?

ios - 添加新的自定义 View Controller 后应用程序无法运行

ios - iOS 如何知道用户来到了特定位置? [既不在后台也不在前景]

ios - 如何使用非可选属性子类化 UITableViewController

View 出现后应用 iOS 8/XCode 6 自动布局约束

swift - 如何用cocoapods提取pod然后在本地使用?