【问题标题】:Custom TableView Error自定义 TableView 错误
【发布时间】:2017-07-30 02:44:26
【问题描述】:

我正在尝试创建自定义表格视图,但偶然发现了每个代码。截至目前,我在下面有这个。这很混乱,可能是错误的,但有人可以帮忙吗?

另外,我不断收到错误代码

在展开可选值时意外发现 nil

import UIKit

class tableOutlineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!


    var names = ["Max", "Bill", "Reagan", "Mikayla", "Jessie", "Sierra", "Jeff", "Erik", "Landon"]
    var numbers = ["35", "33", "29", "27", "25", "23", "19", "15", "11"]
    var photo = [UIImage(named: "Person1"), UIImage(named: "Person2"), UIImage(named: "Groceries"), UIImage(named: "Person3"), UIImage(named: "Person4"), UIImage(named: "Person5"), UIImage(named: "Person6"), UIImage(named: "Person7")]


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell

        cell.images.image = photo[indexPath.row]
        cell.name.text = names[indexPath.row]
        cell.number.text = numbers[indexPath.row]


        return cell
    }

}

【问题讨论】:

  • 你可以在这里添加你的CustomCell类的代码

标签: arrays swift tableview custom-cell


【解决方案1】:

错误在以下行,因为它无法强制解开CustomCell:

let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell

由于您可能将Nib 用于您的自定义单元格,您应该首先将其注册到viewDidLoad。

let nib = UINib(nibName: YOUR_CUSTOM_CELL_NIB_NAME, bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "CustomCell")

【讨论】:

    【解决方案2】:

    似乎您忘记在自定义单元格的 Xib 文件的属性检查器中定义标识符(CustomCell)。因此,当您进行强制解包时,您将获得零。还要避免用力展开以避免崩溃。已重构代码:-

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as? CustomCell else {return UITableViewCell()}
      cell.images.image = photo[indexPath.row]
      cell.name.text = names[indexPath.row]
      cell.number.text = numbers[indexPath.row]
      return cell
     }
    

    【讨论】:

      【解决方案3】:

      首先,请不要使用多个数组填充您的 tableview。这些阵列很容易不同步并导致崩溃。理想情况下,您应该创建一个以姓名、年龄和照片为元素的struct 类。然后你应该有一个结构类数组来填充你的表格视图。

      在这里,在您的名称数组中,您有 9 个元素,而您的图像数组有 8 个元素。这会导致您的应用程序崩溃,因为 indexpath 方法中的行单元格无法找到第 9 个图像。

      cell.images.image = photo[indexPath.row]
      

      【讨论】:

        【解决方案4】:

        所以我听取了大家的建议,当我运行下面的代码时,我得到了零错误。但是在模拟器中,它显示了一个空的 tableView,而我放在 tableView 上方的图像是来自另一个 View Controller 的另一个图像。不知道是密码还是别的什么。

            let nib = UINib(nibName: "CustomCell", bundle: nil)
            tableView.register(nib, forCellReuseIdentifier: "Cell")
        }
        
        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return names.count
        }
        
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell else {return UITableViewCell()}
            cell.images.image = photo[indexPath.row]
            cell.name.text = names[indexPath.row]
            cell.numbers.text = numbers[indexPath.row]
            return cell
        

        还有我的 CustomCell

        import UIKit
        
        class CustomCell: UITableViewCell {
        
        @IBOutlet weak var images: UIImageView!
        @IBOutlet weak var name: UILabel!
        @IBOutlet weak var numbers: UILabel!
        
        
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
        
            // Configure the view for the selected state
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多