简体   繁体   English

用数组中的数据实现静态表视图单元

[英]Implementing static table view cells with data in an array

I have some data which is pulled from a DB and stored in an array within my UITableViewController . 我有一些数据从数据库中提取并存储在UITableViewController的数组中。 However, when I try to put the data inside each of my cells, I get an error that my index is out of the bounds of the array. 但是,当我尝试将数据放入每个单元格中时,出现一个错误,我的索引超出了数组的范围。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellItems.count
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let item: ItemPreBasketModel = cellItems[indexPath.row] as! ItemPreBasketModel
    if indexPath.row == 0 {
        //Desc cell
        let cellIdentifier: String = "descCell"
        let descCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return descCell
    } else if indexPath.row == 1 {
        //Price cell
        let cellIdentifier: String = "priceCell"
        let priceCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return priceCell
    } else if indexPath.row == 2 {
        //Quantity cell
        let cellIdentifier: String = "quantityCell"
        let quantityCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return quantityCell
    } else {
        //Submit cell
        let cellIdentifier: String = "submitCell"
        let submitCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return submitCell
    }
}

Here is the cellItems variable and also where it is populated: 这是cellItems变量,也是它的填充位置:

var cellItems: NSArray = NSArray() {
    didSet {
        tableView.reloadData()
    }
}

    func itemsDownloaded(items: NSArray) {
    cellItems = items
    tableView.reloadData()
}

Here is the static table view in the storyboard: 这是情节提要中的静态表格视图:

表格视图的设置

What I want to do, which I've took out of the code for as it still fails before reaching this part, is to use the 'item' variable I declare inside cellForRowAt and populate each of my cell outlets with a part of the object, 我想要做的是使用代码,因为它在到达这一部分之前仍然失败,因此我从代码中删除了它,它是使用在cellForRowAt内部声明的“ item”变量,并用对象的一部分填充每个单元格出口,

ie: descCell.descLabel.text = item.name 即: descCell.descLabel.text = item.name

The error I get is: 我得到的错误是:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 线程1:致命错误:展开一个可选值时意外地找到零

on the line 在线上

let descCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!

You should not use a table view controller with static cells and cellForRowAt at the same time. 您不应将表视图控制器与静态单元格和cellForRowAt同时使用。 You should use dynamic prototypes to populate an array dynamically. 您应该使用动态原型动态填充数组。

To change the content type of your table view, select it in Interface Bulder, open the Attributes inspector (the fourth icon from the left in the right sidebar) and select Dyanmic Prototypes for Content under the Table View section. 要更改表格视图的内容类型,请在Interface Bulder中将其选中,打开“属性”检查器 (右侧栏中左侧第四个图标),然后在“ 表格视图”部分下选择“ 内容的动态 原型 ”。

属性检查器中的“动态原型”设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM