Ios 如果设置UITableViewCell的detailTextLabel,应用程序将崩溃
我想在UITableViewCell的datailTextLabel中设置一个值。如果代码运行,应用程序就会崩溃 这是错误:Ios 如果设置UITableViewCell的detailTextLabel,应用程序将崩溃,ios,uitableview,swift,detailtextlabel,Ios,Uitableview,Swift,Detailtextlabel,我想在UITableViewCell的datailTextLabel中设置一个值。如果代码运行,应用程序就会崩溃 这是错误:致命错误:在展开可选值时意外发现nil 这是发生错误的方法: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWit
致命错误:在展开可选值时意外发现nil
这是发生错误的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.detailTextLabel!.text = "Date" //error occurs
return cell
}
可能有用的信息:
使用:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
而且,不需要强制展开它。即使存在
detailtextlab
,我也会使用cell.detailtextlab?.text=“Date”
来确保安全,并确保您的单元格是支持detailtextlab
的样式。例如,左细节,右细节,副标题。我没有使用故事板。如何以编程方式更改样式?UITableViewCellStyle(样式:UITableViewCellStyle.Value1,reuseIdentifier:“Cell”)
虽然有更多用于以编程方式创建单元格的设置,但有联机资源可供查找