3

I have two kinds of design in single UITableViewCell. Here is the design which I want and getting at the time of loading viewController

Correct cell design But after scrolling I'm getting following result. InCorrect design

I think this problem is caused by reusability of UITableViewCell. here is my code of 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
    }

So please help me to solve this issue. Thanks in Advance.

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
NiravS
  • 1,144
  • 1
  • 12
  • 24
  • I think issue in `arrayNotificationList `. Please debug and check your array values. – dahiya_boy Feb 23 '17 at 13:08
  • @nirav you can use two cell easily manage. – Nidhi Patel Feb 23 '17 at 13:09
  • 3
    `cell.lblTime.isHidden = true` you don't do `cell.lblTime.isHidden = false` in the else case. Each time you modify a property, think about what to do in the other case : hide/show/change value, etc. Else, you can override `prepareForReuse()` to do that, and hide/show/reset value. – Larme Feb 23 '17 at 13:09
  • Many many Thanks @Larme. It's help me a lot. – NiravS Feb 23 '17 at 13:27
  • Please post this as answer so I can mark as accepted.@Larme – NiravS Feb 23 '17 at 13:29

1 Answers1

2

The problem is that when you scrolled, the

        cell.lblTime.isHidden = true

line hid the label in the cell. When it was reused, it was still hidden so the middle label enlarged to fill up the remaining space.

The solution is to either,

A. Create another cell subclass which cleans up the code considerably and it would no longer be necessary to show or hide labels.

B. Make sure to set

cell.lblTime.isHidden = false

in the else clause.

I hope this helps.

Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23