【问题标题】:Setting tableView datasource is causing app to crash设置 tableView 数据源导致应用程序崩溃
【发布时间】:2021-07-10 00:45:47
【问题描述】:

我正在尝试使用自定义单元格创建表格视图,但是每当我将数据源合并到视图控制器中时,应用程序崩溃,我知道这会导致应用程序崩溃,因为当我注释掉 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")
}

}

【问题讨论】:

  • 什么是崩溃信息?它在哪条线上崩溃?简单地分配数据源不会导致崩溃。您的数据源函数之一存在错误。异常消息和行将有助于理解它是什么。
  • 我在任何地方都看不到您根据重用标识符注册单元类。这可能会导致您的崩溃。此外,如果您坚持 Swift 约定,它将有助于您的代码的可读性;类型名称(类和结构)应以大写字母开头。变量和属性应以小写字母开头。另外,使用驼峰式,不要使用下划线,所以eventsListTable 不是EventsList_Table
  • 控制台中没有错误消息,应用程序只是没有像在完全黑屏中那样加载,但是如果我不分配数据源,应用程序会加载,但表格不希望有一些帮助跨度>
  • 我也在ListView.EventsList_Table.register(CustomCell.self, forCellReuseIdentifier: "EventsCell")这一行注册自定义单元格
  • 好的。我现在看到了。你的代码到处都是。您的应用程序没有崩溃。显示黑屏不是崩溃。如果应用程序崩溃,您将被弹回跳板并收到异常消息。您可能有布局问题。

标签: ios swift uitableview uiviewcontroller swift5


【解决方案1】:

由于您将单元格的框架设置为layoutSubviews,因此您正在导致您的应用程序陷入无限循环。设置框架将使单元格的布局无效并导致再次调用layoutSubviews,您设置框架的位置等等。

注释行

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

防止无限循环。

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

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

【讨论】:

  • 谢谢你解决了这个问题
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-06-04
    • 2011-08-24
    • 2014-05-03
    相关资源
    最近更新 更多