3
我在单个UITableViewCell
中有两种设计。这是我想要的设计,并在加载时获取viewController在UITableView单元重复使用单元格中有问题
我觉得这个问题是由UITableViewCell的可重用性造成的。这里是我的代码cellForRowAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "notificationCell", for: indexPath) as! NotificationTableViewCell
cell.selectionStyle = .none
cell.btnClose.tag = indexPath.row
let noti_flag = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "noti_flag") //as! String
let noti_flag_string = NSString(format: "%@", noti_flag as! CVarArg) as String
cell.lbl_From.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "caller_id") as? String ?? ""
if noti_flag_string == "0" {
cell.lblTime.isHidden = true
cell.imgThumbIcon.isHidden = true
cell.lbl_remaining.isHidden = true
cell.lbl_mm_text.text = "Missed call"
cell.lbl_mm_text.font = cell.lbl_mm_text.font.withSize(23)
}
else{
var startAttributedText = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "rebound_start_time") as! String
var endAttributedText = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "rebound_end_time") as! String
startAttributedText = Model.shared.convertLocalTimeToServer(timeString: startAttributedText,isTimeFromServer: true)
endAttributedText = Model.shared.convertLocalTimeToServer(timeString: endAttributedText,isTimeFromServer: true)
let dateString:String = startAttributedText + " - " + endAttributedText
cell.lblTime.attributedText = convertStringToAttr(dateString: dateString)
cell.lbl_remaining.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "remaining_time") as? String ?? ""
cell.lbl_mm_text.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "mm_text") as? String ?? ""
//cell.lbl_From.text = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "caller_id") as? String ?? ""
let mm_type = (arrayNotificationList.object(at: indexPath.row) as! NSDictionary).object(forKey: "mm_type") as! String//"img"
switch mm_type {
case "img":
cell.imgThumbIcon.image = UIImage(named: "thumb_camera")
case "vid":
cell.imgThumbIcon.image = UIImage(named: "thumb_video")
case "aud":
cell.imgThumbIcon.image = UIImage(named: "thumb_audio")
case "str":
cell.imgThumbIcon.image = UIImage(named: "thumb_sticker")
case "txt":
cell.imgThumbIcon.image = UIImage(named: "thumb_text")
case "brd":
cell.imgThumbIcon.image = UIImage(named: "thumb_brand")
default:
cell.imgThumbIcon.image = UIImage(named: "thumb_camera")
}
}
return cell
}
所以请帮我解决这个问题。提前致谢。
我认为'arrayNotificationList'的问题。请调试并检查您的数组值。 –
@nirav你可以使用两个单元格轻松管理。 –
'cell.lblTime.isHidden = true'你不会在else情况下执行'cell.lblTime.isHidden = false'。每次修改属性时,都要考虑在其他情况下应该如何操作:隐藏/显示/更改值等。否则,可以重写'prepareForReuse()'来执行此操作,并隐藏/显示/重置值。 – Larme