为 UITableViewCell 展开 Optionals
Unwrapping Optionals for UITableViewCell
我有以下代码。我正在使用所有可能的 Xcode 建议以及关于 SO 等的各种来源,但我似乎无法纠正可选问题:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell?
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:identifier as String)
cell.backgroundColor = UIColor.clearColor()
// ERROR HERE
}
cell.textLabel?.text = dataArray.objectAtIndex(indexPath.row).valueForKey("category_name") as! String
// ERROR HERE
var str = String(format: "%@%@%@",kServerURl,"/upload/",dataArray.objectAtIndex(indexPath.row).valueForKey("category_image") as! String)
cell?.imageView?.image = UIImage(data: NSData(contentsOfURL: NSURL(string:str)!)!)
// ERROR HERE
return cell
//ERROR HERE
错误:
VALUE OF OPTIONAL TYPE UITABLEVIEWCELL NOT UNWRAPPED DID YOU MEAN TO USE ! or ?
用了没关系!要么 ?我得到同样的错误,在某些情况下,如果有两个错误就会解决!正在添加层单元!!.
cell 变量是 UITableViewCell? 类型的可选变量,因此您必须在使用它之前解包它。您可能应该去阅读 the documentation on Optional Types 以熟悉它们的使用。像这样的行:
cell.backgroundColor = UIColor.clearColor()
应该是:
cell!.backgroundColor = UIColor.clearColor()
或:
if let someCell = cell {
someCell.backgroundColor = UIColor.clearColor()
}
在您知道实例不是 nil
的情况下,您将使用第一种解包,就像在 nil
检查 if 语句之后直接使用一样。如果您不确定它不是 nil
.
,您将使用第二种展开方式
问题是你有一个双重可选:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell?
as?
表示转换可能会失败,因此它将您正在转换的值包装在一个可选值中。您要转换的值是 也是 一个可选值 (String?
)。因此,如果您在调试器中查看 cell 的值,您会看到如下内容:
Optional(Optional(<UITableViewCell:0x14f60bb10
您可以通过以下方式显式解包:
cell!!
(两次感叹),但这有点脏。相反,您只需要这样的演员之一:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell
注意我删除了最后一个问号。那么你可以这样做:
cell!.backgroundColor = UIColor.clearColor()
最后一个选择是首先用感叹号强行打开它:
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as! UITableViewCell
那么您只需要:
cell.backgroundColor = UIColor.clearColor()
我有以下代码。我正在使用所有可能的 Xcode 建议以及关于 SO 等的各种来源,但我似乎无法纠正可选问题:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell?
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:identifier as String)
cell.backgroundColor = UIColor.clearColor()
// ERROR HERE
}
cell.textLabel?.text = dataArray.objectAtIndex(indexPath.row).valueForKey("category_name") as! String
// ERROR HERE
var str = String(format: "%@%@%@",kServerURl,"/upload/",dataArray.objectAtIndex(indexPath.row).valueForKey("category_image") as! String)
cell?.imageView?.image = UIImage(data: NSData(contentsOfURL: NSURL(string:str)!)!)
// ERROR HERE
return cell
//ERROR HERE
错误:
VALUE OF OPTIONAL TYPE UITABLEVIEWCELL NOT UNWRAPPED DID YOU MEAN TO USE ! or ?
用了没关系!要么 ?我得到同样的错误,在某些情况下,如果有两个错误就会解决!正在添加层单元!!.
cell 变量是 UITableViewCell? 类型的可选变量,因此您必须在使用它之前解包它。您可能应该去阅读 the documentation on Optional Types 以熟悉它们的使用。像这样的行:
cell.backgroundColor = UIColor.clearColor()
应该是:
cell!.backgroundColor = UIColor.clearColor()
或:
if let someCell = cell {
someCell.backgroundColor = UIColor.clearColor()
}
在您知道实例不是 nil
的情况下,您将使用第一种解包,就像在 nil
检查 if 语句之后直接使用一样。如果您不确定它不是 nil
.
问题是你有一个双重可选:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell?
as?
表示转换可能会失败,因此它将您正在转换的值包装在一个可选值中。您要转换的值是 也是 一个可选值 (String?
)。因此,如果您在调试器中查看 cell 的值,您会看到如下内容:
Optional(Optional(<UITableViewCell:0x14f60bb10
您可以通过以下方式显式解包:
cell!!
(两次感叹),但这有点脏。相反,您只需要这样的演员之一:
var cell =
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as? UITableViewCell
注意我删除了最后一个问号。那么你可以这样做:
cell!.backgroundColor = UIColor.clearColor()
最后一个选择是首先用感叹号强行打开它:
tableview!.dequeueReusableCellWithIdentifier(identifier as String) as! UITableViewCell
那么您只需要:
cell.backgroundColor = UIColor.clearColor()