2016-10-03 70 views
1

我想設置一個表格視圖,它將根據頂部的分段控制器更改單元格。但是,在重新加載tableview時試圖更改單元格時,實際上我有一個返回函數,我收到了一個返回函數錯誤。我能做些什麼來解決這個問題?缺少函數返回'UITableViewCell',但實際返回兩次

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 




    if friendSelector.selectedSegmentIndex == 0 { 
     print("0") 

     cell = self.friendsTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FriendsTableViewCell 


     cell.nameLabel.text = friends[indexPath.row] 
     cell.bacLabel.text = String(friendsBac[indexPath.row]) 
     cell.statusImageView.image = friendsImage[indexPath.row] 

     return cell 

    } 

    if friendSelector.selectedSegmentIndex == 1 { 
     print("1") 

     celladd = self.friendsTable.dequeueReusableCell(withIdentifier: "celladd", for: indexPath) as! FriendsAddTableViewCell 

     celladd.nameLabel.text = requested[indexPath.row] 
     celladd.statusImageView.image = UIImage(named: "greenlight") 

     return celladd 


    } 

} 

View of the Table With Two different Custom UITableViewCells

+2

如果兩個條件都不令人滿意,則該方法不返回任何內容。 –

回答

3

您應該返回一個單元格。在上面的代碼中,如果兩個條件均失敗,則不會返回任何內容。所以提出了一個警告。只需刪除第二個「if」條件並使用其他情況如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if friendSelector.selectedSegmentIndex == 0 { 
     print("0") 

     cell = self.friendsTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FriendsTableViewCell 


     cell.nameLabel.text = friends[indexPath.row] 
     cell.bacLabel.text = String(friendsBac[indexPath.row]) 
     cell.statusImageView.image = friendsImage[indexPath.row] 

     return cell 

    } 

    else { 
     print("1") 

     celladd = self.friendsTable.dequeueReusableCell(withIdentifier: "celladd", for: indexPath) as! FriendsAddTableViewCell 

     celladd.nameLabel.text = requested[indexPath.row] 
     celladd.statusImageView.image = UIImage(named: "greenlight") 

     return celladd 


    } 

} 
+0

謝謝!我不能相信我錯過了這一點。 –

+1

您隨時歡迎... – KSR

1

這是非常明顯的。如果條件有兩個返回語句。如果你的'如果'條件不被執行會怎麼樣?這種情況沒有返回聲明。這就是爲什麼編譯器抱怨

1

你有兩個if語句這兩個可能不是真實的,所以你必須返回所選擇的指標既不是0或1

if friendSelector.selectedSegmentIndex == 0 { 
    ... 
    return cell 
} 
else if friendSelector.selectedSegmentIndex == 1 { 
    ... 
    return celladd 
} 
return UITableViewCell() 

我時的電池寧願爲這種事情使用switch語句。

switch friendSelector.selectedSegmentIndex { 
case 0: 
    ... 
    return cell 
case 1: 
    ... 
    return celladd 
default: 
    return UITableViewCell() 
}