我如何从表格视图中删除空单元格

时间:2019-04-21 10:14:57

标签: ios swift xcode

我将自定义单元格用于表视图,并且根据我的代码,如果不满足任何条件,它将在表视图中添加一个空单元格或行。我是iOS上的新手,请帮助我删除空白单元格。这是我的代码段。

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

        let dict1 = arrMsg.object(at: indexPath.row) as! NSDictionary

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChatTableViewCell
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "Cell2") as! Chat2TableViewCell

        if((String(describing: dict1.object(forKey: "SenderId")!)) == Auth.auth().currentUser?.uid && (ChatVC.selectedUser.replacingOccurrences(of: ".", with: "") == (String(describing: dict1.object(forKey: "ReceiverId")!))))
        {
            cell2.lblSender.text = (dict1.object(forKey: "Message") as! String)
            cell2.lblSender.backgroundColor = UIColor(red: 0.09, green: 0.54, blue: 1, alpha: 1)
            cell2.lblSender.font = UIFont.systemFont(ofSize: 18)
            cell2.lblSender.textColor = .white
            cell2.lblSender?.layer.masksToBounds = true
            cell2.lblSender.layer.cornerRadius = 7
            return cell2
        }
        else if ((String(describing: dict1.object(forKey: "ReceiverId")!)) == (Auth.auth().currentUser!.email?.replacingOccurrences(of: ".", with: ""))!)
        {
            cell.lblReceiver.text = (dict1.object(forKey: "Message") as! String)
            cell.lblReceiver.backgroundColor = UIColor .lightGray
            cell.lblReceiver.font = UIFont.systemFont(ofSize: 18)
            cell.lblReceiver.textColor = UIColor.white
            cell.lblReceiver?.layer.masksToBounds = true
            cell.lblReceiver.layer.cornerRadius = 7
            return cell
        }
        else {
            let cell = UITableViewCell()
            return cell
        }
    }

3 个答案:

答案 0 :(得分:1)

我认为这应该由您的TableView's DataSource来解决,而不是您必须处理cellForRowAt中是否存在某些东西的逻辑。您应该能够添加逻辑来根据您的arrMsg对象找到每个节中的行数/有多少节

答案 1 :(得分:0)

您需要

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

    let dict1 = arrMsg.object(at: indexPath.row) as! [String:Any]
    let str = dict1["SenderId"] as! String
    if str == Auth.auth().currentUser?.uid { 
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "Cell2") as! Chat2TableViewCell
        cell2.lblSender.text = (dict1.object(forKey: "Message") as! String)
        cell2.lblSender.backgroundColor = UIColor(red: 0.09, green: 0.54, blue: 1, alpha: 1)
        cell2.lblSender.font = UIFont.systemFont(ofSize: 18)
        cell2.lblSender.textColor = .white
        cell2.lblSender?.layer.masksToBounds = true
        cell2.lblSender.layer.cornerRadius = 7
        return cell2
    }
    else
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChatTableViewCell
        cell.lblReceiver.text = (dict1.object(forKey: "Message") as! String)
        cell.lblReceiver.backgroundColor = UIColor .lightGray
        cell.lblReceiver.font = UIFont.systemFont(ofSize: 18)
        cell.lblReceiver.textColor = UIColor.white
        cell.lblReceiver?.layer.masksToBounds = true
        cell.lblReceiver.layer.cornerRadius = 7
        return cell
    }

}

您的工作是确保senderID应该是当前用户还是发送用户,但不能为nil,这将导致返回空单元格,也不要在Swift中使用NS

答案 2 :(得分:0)

转到情节提要中,使用检查更改分隔符样式从默认值或其他任何值中选择表视图

相关问题