设置 tableView 数据源导致应用程序崩溃

Setting tableView datasource is causing app to crash

我正在尝试使用自定义单元格创建一个 table 视图,但是每当我将数据源合并到 viewcontroller 应用程序崩溃时,我知道这就是崩溃应用程序的原因,因为当我注释掉时EventsList.EventsList_Table.datasource = self 应用程序运行但自定义单元格不显示我也尝试过仅从 UITableViewController 继承,但这也会使应用程序崩溃或至少不会加载并显示黑屏。

CustomCell.swift

class CustomCell: UITableViewCell {



override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    
    self.layer.masksToBounds = false
    self.backgroundColor = .white
    self.layer.shadowOpacity = 0.15
    self.layer.shadowRadius = 6
    self.layer.shadowColor = UIColor.lightGray.cgColor
    self.layer.cornerRadius = 6

    
}

override func layoutSubviews() {
    super.layoutSubviews()
    
    self.frame = self.frame.inset(by: UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10))
    
}

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

}

EventsListViewController.swift

class EventsListViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {

var ListView: EventsListView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .white
        ListView = EventsListView(frame: CGRect(x: 0, y: 0, width: screenSize().width, height: screenSize().height))
        ListView.EventsList_Table.register(CustomCell.self, forCellReuseIdentifier: "EventsCell")
        ListView.EventsList_Table.separatorStyle = UITableViewCell.SeparatorStyle.none
        ListView.EventsList_Table.delegate = self
        ListView.EventsList_Table.dataSource = self
    self.view.addSubview(ListView)
}

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

        return 1
    
    }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let Cell =  ListView.EventsList_Table.dequeueReusableCell(withIdentifier: "EventsCell", for : indexPath) as! CustomCell
            Cell.selectionStyle = .none
            Cell.layoutSubviews()
        return Cell
}

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

}

如果这对诊断问题有任何帮助,这是视图的代码

class EventsListView: UIView {


var Events_Title: UILabel = {
    var Title = UILabel()
    Title.translatesAutoresizingMaskIntoConstraints = false
    Title.textColor = UIColor(red: 70/256, green: 56/256, blue: 83/256, alpha: 1.0)
    Title.textAlignment = .left
    Title.font = UIFont(name: "GTEestiDisplay-Medium", size: 36)
    Title.text = "Events"
    return Title
}()

var EventsList_Table: UITableView = {
    var Table = UITableView()
    Table.translatesAutoresizingMaskIntoConstraints = false
    Table.backgroundColor = .white
    return Table
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    
    self.addSubview(Events_Title)
    
    Events_Title.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    Events_Title.topAnchor.constraint(equalTo: self.topAnchor, constant: 50).isActive = true
    Events_Title.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 30).isActive = true
    
    self.addSubview(EventsList_Table)
    
    EventsList_Table.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    EventsList_Table.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.9).isActive = true
    EventsList_Table.topAnchor.constraint(equalTo: Events_Title.bottomAnchor, constant: 25).isActive = true
    
    
    
}

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

}

您正在导致您的应用陷入无限循环,因为您在 layoutSubviews 中设置了单元格的框架。设置框架将使单元格的布局无效并导致再次调用 layoutSubviews,在您设置框架的地方等等。

注释行

self.frame = self.frame.inset(by: UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10))

防止无限循环。

我强烈建议您在整个应用程序中始终通过约束采用自动布局。

使用约束时,您应该使用 leadingtrailing 锚点而不是 leftright,因为这将使您的应用能够适应从右到左的方向语言环境自动。无论区域设置如何,仅当您需要在左侧或右侧布置某些内容时才使用 leftright

CustomCell layoutSubviews导致死循环,可以设置

override func layoutSubviews() {
   super.layoutSubviews()
   contentView.frame = frame.inset(by: UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10))
}