简体   繁体   English

对自定义UITableView单元进行故障排除[快速]

[英]Troubleshooting Custom UITableView Cell [Swift]

I have created a custom UITableView cell in my Storyboard that looks like this: 我在故事板中创建了一个自定义UITableView单元格,如下所示:

在此处输入图片说明

I hooked it up to my UITableViewCell class like this: 我将其连接到我的UITableViewCell类,如下所示:

import UIKit 导入UIKit

class StatusCell: UITableViewCell {

@IBOutlet weak var InstrumentImage: UIImageView!

@IBOutlet weak var InstrumentType: UILabel!

@IBOutlet weak var InstrumentValue: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}
}

Finally I attempted to initialize the UITableView from my UIViewController as such: 最后,我尝试从UIViewController初始化UITableView,如下所示:

import UIKit

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var TableView: UITableView!

let Items = ["Altitude","Distance","Groundspeed"]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.Items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: StatusCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as StatusCell!

    cell.InstrumentType?.text = Items[indexPath.row]
    cell.InstrumentValue?.text = "150 Km"
    cell.InstrumentImage?.image = UIImage(named: Items[indexPath.row])
    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

}

However, when I attempt to run the program I get an EXC_BAD_INSTRUCTION error: 但是,当我尝试运行程序时,出现EXC_BAD_INSTRUCTION错误:

在此处输入图片说明

What could I have done wrong? 我做错了什么? Any help would be appreciated! 任何帮助,将不胜感激!

The debugger output shows that cell is nil , meaning it could not be instantiated. 调试器输出显示cellnil ,这意味着无法实例化。 Furthermore, you are forcing an unwrapping of the optional (using the ! ) which causes the app to crash on the nil value. 此外,您正在强制展开可选项(使用! ),这会导致应用程序在nil值上崩溃。

Try to change your cellForRowAtIndexPath method like so (notice the dequeueReusableCellWithIdentifier method): 尝试像这样更改您的cellForRowAtIndexPath方法(注意dequeueReusableCellWithIdentifier方法):

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as StatusCell

    cell.InstrumentType.text = Items[indexPath.row]
    cell.InstrumentValue.text = "150 Km"
    cell.InstrumentImage.image = UIImage(named: Items[indexPath.row])

    return cell
}

Assuming that your custom tableViewCell class is correctly set up and the outlets are bound, there is no need to check for optionals. 假设您的自定义tableViewCell类已正确设置并且插座已绑定,则无需检查可选项。

Place a breakpoint on the let cell = ... line and step through the code. let cell = ...行上放置一个断点,然后逐步执行代码。 Check if cell gets initialized and is not nil . 检查cell是否已初始化并且不是nil

And please: Do not use upper case names for properties and variables (your outlets, Items array...) as upper case names are for classes, structs, ... 并且请:不要将大写字母的名称用于属性和变量(出口, Items数组等),因为大写字母的名称用于类,结构等。

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

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