Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 2.0中CellForRowatineXpath的正确实现_Ios_Swift_Uitableview - Fatal编程技术网

Ios Swift 2.0中CellForRowatineXpath的正确实现

Ios Swift 2.0中CellForRowatineXpath的正确实现,ios,swift,uitableview,Ios,Swift,Uitableview,以下CellForRowatineXpath的实现是否是技术上正确的最佳实践方式,并考虑到了选项的展开 class MyTableViewController: UITableViewController { var cell : UITableViewCell? // other methods here override func tableView(tableView: UITableView, cellForRowAtIndexPath indexP

以下CellForRowatineXpath的实现是否是技术上正确的最佳实践方式,并考虑到了选项的展开

 class MyTableViewController: UITableViewController {
        var cell : UITableViewCell?
         // other methods here 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        cell = tableView.dequeueReusableCellWithIdentifier("ItemCell")! as UITableViewCell
        let myItem = items[indexPath.row]

        cell!.textLabel?.text = myItem.name
        cell!.detailTextLabel?.text = myItem.addedByUser

        return cell!
      }

    }

在Swift 2中,
dequeueReusableCellWithIdentifier
声明为

func dequeueReusableCellWithIdentifier(_ identifier: String,
                      forIndexPath indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, 
             cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
cellforrowatinexpath
被声明为

func dequeueReusableCellWithIdentifier(_ identifier: String,
                      forIndexPath indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, 
             cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
你看,没有选择

代码可以简化为

class MyTableViewController: UITableViewController {

   // other methods here 
   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath)
     let myItem = items[indexPath.row]

     cell.textLabel?.text = myItem.name
     cell.detailTextLabel?.text = myItem.addedByUser

     return cell
  }
}
如果是自定义表格视图单元格,则可以强制将该单元格转换为自定义类型

let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! CustomCell

选择单击符号或使用快速帮助查找确切的签名总是一个好主意。

没有理由在方法外部有可选的单元格变量,只需在方法内部创建一个非可选的局部变量提示:使用“Master Detail application”创建一个新的Xcode应用程序如果我尝试让cell=tableView.dequeueReusableCellWithIdentifier(“ItemCell”)作为UITableViewCell而不展开,编译器建议我使用!,如let cell=tableView.dequeueReusableCellWithIdentifier(“ItemCell”)!作为UITableViewCell,您的代码可以正常工作!!您不需要展开任何内容,因为所有相关参数都是非可选的。您说过要使用第二种方法来获取索引路径参数,但您的示例仍然使用错误的方法。许多lint工具都不建议使用“as!”强制展开。有什么更好的方法?@Satyam Force unwrapping本身并不是邪恶的。不仅仅是黑与白。在这种特殊情况下,甚至建议强制展开,因为它会显示设计错误。如果任何东西在Interface Builder中正确连接,则代码不得崩溃。