简体   繁体   English

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

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

I'm using custom cells for table view and as per my code if no condition met it adds an empty cell or row in my table view. 我将自定义单元格用于表视图,根据我的代码,如果不满足任何条件,它将在表视图中添加一个空单元格或行。 i'm new on iOS please help me in removing empty cells. 我是iOS上的新手,请帮助我删除空白单元格。 here is my code snippet. 这是我的代码段。

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
        }
    }

I think this is something that should be cleared up by your TableView's DataSource instead of you having to handle the logic of whether something exists or not in cellForRowAt . 我认为这应该由TableView的DataSource清除,而不是必须处理cellForRowAt中是否存在某些内容的逻辑。 You should be able to add logic to find the number of rows in each section/how many sections there are based upon your arrMsg object 您应该能够添加逻辑来根据您的arrMsg对象查找每个节中的行数/有多少节

You need 你需要

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
    }

}

it's your job to make sure that senderID should be the current user or the sending user , but not nil which will lead to returning the empty cell , also don't use NS stuff in Swift 确保您的senderID应该是当前用户还是发送用户是您的工作,而不是nil,这将导致返回空单元格,也不要在Swift中使用NS内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表格视图中删除空单元格? iOS Swift - How do I remove empty cells in table view? iOS Swift 如何更优雅地在表格视图中添加/删除单元格? - How can I add/remove cells in a table view more elegantly? 如果我将UISwitch控件添加到每个表视图单元格中,如何判断它属于哪个单元格? - If I add a UISwitch control to each of my table view cells, how can I tell which cell it belongs to? 如何在iOS中停止表格视图单元格多次加载其子视图? - How can I stop my table view cells loading their subviews multiple of times in iOS? 从表格视图单元格中删除UIlabel文本 - Remove the UIlabel text from table view cells 是否可以通过Interface Builder删除表格视图底部的空白单元格 - Is it possible to remove empty cells at the bottom of the table view via Interface Builder 如何轻按按钮即可撤消对表格视图单元格的重新排序 - How can I undo the reordering of table view cells on the tap of a button 如何获取 Swift 中表格视图单元格数据的总和? - How can I take the sum of table view cells' data in Swift? 如何自定义显示在不同表格视图单元格中的图像? - How can I customize the image displayed in different table view cells? 如何快速订购两个动态表格视图单元格? - How can I order two dynamic table view cells in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM