ios - 我如何从我的表格 View 中删除空单元格

标签 ios swift xcode

我正在为 TableView 使用自定义单元格,如果没有满足条件,它会根据我的代码在我的 TableView 中添加一个空单元格或行。我是 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's DataSource 解决而不是您必须处理 cellForRowAt 中是否存在某些东西的逻辑。您应该能够添加逻辑以根据您的 arrMsg 对象找到每个部分中的行数/有多少部分

关于ios - 我如何从我的表格 View 中删除空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55781831/

相关文章:

arrays - 如何从 Swift ArraySlice 中获取第 i 个元素?

ios - 每次都恢复状态?

ios - 如何在 Xcode 中访问 Genstrings?

ios - 有没有办法在 iPhone 上获取启动的 VPN 隧道的套接字描述符,以便我可以在 C++ 库中使用它

ios - 线程 0 因 ARM 线程状态(64 位)而崩溃

ios - 如何在应用程序启动时使用首选项 plist 在 iPhone 4 iOS 6 中切换手电筒功能

objective-c - 在xcode中启用标签?或者减轻没有它们的痛苦?

iOS 加载自定义注解

ios - 在 iOS 8/9 中根据设备方向更改 Viewcontroller

ios - 访问 iPhone 模拟器根目录和主目录