2015-07-11 60 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 
} 
相關問題