Joh*_* II 3 uitableview ios swift
我试图建立一个表视图,该视图将根据顶部的分段控制器来更改单元格。但是,在尝试重新加载tableview时尝试更改单元格时,实际上我有一个return函数时,我收到了return函数错误。我该怎么做才能解决此问题?
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
}
}
Run Code Online (Sandbox Code Playgroud)
您有两个 if 语句,两者都可能不为 true,因此在所选索引既不是 0 也不是 1 的情况下,您必须返回一个单元格。
if friendSelector.selectedSegmentIndex == 0 {
...
return cell
}
else if friendSelector.selectedSegmentIndex == 1 {
...
return celladd
}
return UITableViewCell()
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用 switch 语句来处理这种事情。
switch friendSelector.selectedSegmentIndex {
case 0:
...
return cell
case 1:
...
return celladd
default:
return UITableViewCell()
}
Run Code Online (Sandbox Code Playgroud)
您应该返回一个单元格。在上面的代码中,如果两个条件都失败,那么将不会返回任何内容。因此提出了警告。只需删除第二个“ if”条件并使用else情况,如下所示:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3692 次 |
| 最近记录: |