2015-07-11 57 views
1

我想通过使用Swift在Xcode中创建一个TO-DO列表应用程序,并且遇到编写“如果let path = indexPath {“表示”条件绑定中的绑定值必须是可选类型“的行。条件绑定中的绑定值必须为可选类型[IOS - SWIFT]

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 
    if let path = indexPath {    
     let currentString = dataSource[path.section][path.row] 
     cell.textLabel?.text = currentString    
    }   
    return cell 
} 
+0

'indexPath'不是可选的,所以不需要解开它。 – Paulw11

回答

0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 
     cell.textLabel?.text = dataSource[indexPath.section][indexPath.row] 
     return cell 
} 

使用此代码,因为我觉得我以前的想法是不好的。

您不需要按照Leo建议的条件绑定。

2

因为indexpath不可选,你不需要使用条件结合

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 
    let currentString = dataSource[indexPath.section][indexPath.row] 
    cell.textLabel?.text = currentString 
    return cell 
} 
2

为什么ü要使用两个constant? 修复URE代码:

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

     cell.textLabel?.text = dataSource[indexPath.section][indexPath.row]    
    }   
    return cell 
} 
相关问题