iDeveloper
iDeveloper

Reputation: 150

UITableViewCell content disappears after scrolling

I have a UITableView which gets its data from Parse. It is basically like a feed that has message posts, UIMapView and UIImages. Some posts have only text however, so I've made up 4 different cell types. For all the different combinations of posts.

  1. Text Alone
  2. Image with Text
  3. Image and Map with Text
  4. Map with Text

After that, in my cellForRowAtIndexPath function, I retrieve all the messages from Parse.

This is my function:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cell:MessageTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? MessageTableViewCell
    let cell2:FullTableViewCell! = tableView.dequeueReusableCellWithIdentifier("AllCell", forIndexPath: indexPath) as? FullTableViewCell
    let cell3:ImageTableViewCell! = tableView.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as? ImageTableViewCell
    let cell4:MapTableViewCell! = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as? MapTableViewCell

    var typeOfCell = ""

        if(self.feedData.count > indexPath.row){

            if let message:PFObject = (self.feedData[indexPath.row] as? PFObject)!{
                long = message.objectForKey("LocationLongitude") as! Double
                lat = message.objectForKey("LocationLatitude") as! Double

                if let userImageFile = message["Photos"]{
                    userImageFile.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            let imageData = imageData
                            let image = UIImage(data:imageData!)
                            if(self.long != 0 || self.lat != 0){
                                //ALL INCLUDED

                                cell2.imgView.image = image
                                cell2.txtMessage.text = message.objectForKey("Message") as? String
                                cell2.txtMessage.font = UIFont(name: "Baskerville", size: 18)
                                let dataFormatter:NSDateFormatter = NSDateFormatter()
                                dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
                                cell2.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
                                typeOfCell = "FullCell"

                            }else{
                                //ONLY IMAGE
                                cell3.imgView.image = image
                                cell3.txtMessage.text = message.objectForKey("Message") as? String
                                cell3.txtMessage.font = UIFont(name: "Baskerville", size: 18)
                                let dataFormatter:NSDateFormatter = NSDateFormatter()
                                dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
                                cell3.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
                                //typeOfCell = "ImageCell"
                            }
                        }
                    }

                }else{
                    if(self.long != 0 || self.lat != 0){
                        //MAPVIEW
                        cell4.txtMessage.text = message.objectForKey("Message") as? String
                        cell4.txtMessage.font = UIFont(name: "Baskerville", size: 18)
                        let dataFormatter:NSDateFormatter = NSDateFormatter()
                        dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
                        cell4.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
                        typeOfCell = "MapCell"
                    }else{
                        //ONLY TEXT
                        cell.txtMessage.text = message.objectForKey("Message") as? String
                        cell.txtMessage.font = UIFont(name: "Baskerville", size: 18)
                        let dataFormatter:NSDateFormatter = NSDateFormatter()
                        dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
                        cell.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
                        typeOfCell = "Cell"
                    }
                }
            }
    }
            if(typeOfCell == "FullCell"){
                self.tableView.rowHeight = 861.00
                print("FullCell")
                return cell2
            }else if(typeOfCell == "Cell"){
                print("Cell")
                self.tableView.rowHeight = 133.00
                return cell
            }else if(typeOfCell == "MapCell"){
                print("MapCell")
                self.tableView.rowHeight = 269.00
                return cell4
            }else{
                print("ImageCell")
                self.tableView.rowHeight = 725.00
                return cell3
            }

}

So far so good. When I run my app however, Only the first cell displays an image (if it has one) and the rest do not. Also, when I try to scroll in my TableView, all my images disappear. So basically I can only view two types of cells. Either type 1 or type 4 in the previously mentioned cell types. I've been stuck on this issue for hours and I can't seem to figure it out. Any help is greatly appreciated.

Upvotes: 0

Views: 1706

Answers (1)

Abu Ul Hassan
Abu Ul Hassan

Reputation: 1396

While using tableViews You should use Else conditions for contents of cell as it reuses cell so when you scroll up down it reuses cell. That causes the problem.

Upvotes: 0

Related Questions