2017-02-23 7 views
3

私は2種類のデザインを1つにまとめました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あなたは2つのセルを簡単に管理することができます。 –

+3

'cell.lblTime.isHidden = true' else caseで' cell.lblTime.isHidden = false'を実行しません。プロパティを変更するたびに、hide/show/valueの変更など、他の場合のやり方について考えてください。そうでない場合は、 'prepareForReuse()'をオーバーライドして値を表示/非表示にすることができます。 – Larme

答えて

2

問題は、あなたがスクロールしたときに、

 cell.lblTime.isHidden = true 

ラインがセルにラベルを隠していることです。それが再利用されたとき、まだ隠されていたので、残りのスペースを埋めるために中央のラベルが拡大されました。

ソリューションがどちらかにある、

A.はかなりのコードをクリーンアップしないと、もはやラベルを表示または非表示にすることが必要であろう別の細胞のサブクラスを作成します。

B.は、else節に

cell.lblTime.isHidden = false 

を設定してください。

こちらがお役に立てば幸いです。

関連する問題