我正在尝试设置一个表视图,它将根据顶部的分段控制器更改单元格.但是,在重新加载tableview时尝试更改单元格时,我收到了返回函数错误,而实际上我有一个返回函数.我该怎么做才能解决
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 } }你应该返回一个单元格.在上面的代码中,如果两个条件都失败,则不会返回任何内容.所以提出警告.只需删除第二个“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 } }