gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:25:11 25 4
gpt4 key购买 nike

我的机器有一个有趣的问题。我想制作表格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com