我正在使用自定义单元格进行表格视图,根据我的代码,如果没有条件,它会在我的表格视图中添加一个空单元格或行。我是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
对象添加逻辑来查找每个部分中的行数/有多少部分
你需要
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
的东西
转到故事板在检查更改分隔符样式中选择默认情况下的表视图或从无到有