2017-02-23 97 views
3

我在单个UITableViewCell中有两种设计。这是我想要的设计,并在加载时获取viewController在UITableView单元重复使用单元格中有问题

Correct cell design 但滚动后,我得到以下结果。 InCorrect design

我觉得这个问题是由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 
    } 

所以请帮我解决这个问题。提前致谢。

+0

我认为'arrayNotificationList'的问题。请调试并检查您的数组值。 –

+0

@nirav你可以使用两个单元格轻松管理。 –

+3

'cell.lblTime.isHidden = true'你不会在else情况下执行'cell.lblTime.isHidden = false'。每次修改属性时,都要考虑在其他情况下应该如何操作:隐藏/显示/更改值等。否则,可以重写'prepareForReuse()'来执行此操作,并隐藏/显示/重置值。 – Larme

回答

2

的问题是,当你滚动时,

 cell.lblTime.isHidden = true 

线藏在标签中的单元格。当它被重用时,它仍然隐藏,所以中间标签被放大以填充剩余空间。

的解决方案是,

A.创建这显着清理代码另一小区子类,它不再是必要的,以显示或隐藏的标签。

B.确保设置

cell.lblTime.isHidden = false 

在else子句。

我希望这会有所帮助。