我正在为 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/