簡體   English   中英

我如何從表格視圖中刪除空單元格

[英]How i can remove the empty cells from my table view

我將自定義單元格用於表視圖,根據我的代碼,如果不滿足任何條件,它將在表視圖中添加一個空單元格或行。 我是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
        }
    }

我認為這應該由TableView的DataSource清除,而不是必須處理cellForRowAt中是否存在某些內容的邏輯。 您應該能夠添加邏輯來根據您的arrMsg對象查找每個節中的行數/有多少節

你需要

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內容

轉到情節提要中,在檢查更改分隔符樣式中從默認值或任何其他值中選擇表視圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM