2016-10-03 56 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() 
}