Che*_*hia 4 uitableview swift xcode6
下面的代码是在UItableview中显示单元格的名称.
override func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell
cell.textLabel!.text = "Spring \(indexPath.row + 1)"
return cell
}
Run Code Online (Sandbox Code Playgroud)
有一个编译错误,Xcode建议我将'as'更改为'as!' -
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
as! UITableViewCell
Run Code Online (Sandbox Code Playgroud)
可能有人解释了什么是垂头丧气,为什么我们需要在这种情况下垂头丧气?
因为-dequeueReusableCellWithIdentifier:退货AnyObject(id在Objective-C中).Swift需要你强迫downcast,因为AnyObject可以是任何对象,并且通过强制转换,你告诉编译器忽略当前类型并强制转换为继承行下面的更具体的对象.