Ios UITableViewCell内容在滚动后消失

Ios UITableViewCell内容在滚动后消失,ios,iphone,swift,uitableview,Ios,Iphone,Swift,Uitableview,我有一个UITableView,它从解析中获取数据。它基本上就像一个包含消息帖子、UIMapView和UIImages的提要。然而,有些帖子只有文本,所以我创建了4种不同的单元格类型。对于所有不同的职位组合 仅文本 带文本的图像 带文本的图像和地图 用文本映射 之后,在我的cellforrowatinexpath函数中,我从Parse中检索所有消息 这是我的职责: 重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsind

我有一个UITableView,它从解析中获取数据。它基本上就像一个包含消息帖子、UIMapView和UIImages的提要。然而,有些帖子只有文本,所以我创建了4种不同的单元格类型。对于所有不同的职位组合

  • 仅文本
  • 带文本的图像
  • 带文本的图像和地图
  • 用文本映射
  • 之后,在我的cellforrowatinexpath函数中,我从Parse中检索所有消息

    这是我的职责:

    重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{

        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
                }
    
    }


    到目前为止还不错。但是,当我运行我的应用程序时,只有第一个单元格显示图像(如果有),其余单元格不显示。此外,当我尝试在TableView中滚动时,所有图像都会消失。所以基本上我只能看到两种类型的细胞。上述单元格类型中的类型1或类型4。我已经在这个问题上纠缠了好几个小时,但我似乎无法理解。非常感谢您的帮助。

    在使用TableView时,您应该使用单元格内容的其他条件,因为它会重用单元格,所以当您上下滚动时,它会重用单元格。这就导致了问题。

    不要像在方法开始时那样将4个单元格出列,找出需要的单元格类型,然后只将该单元格出列。也不要在此方法中设置tableview的
    行高,实现
    tableView:heightforrowatinedexpath:
    并返回正确的行高度。在
    cellforrowatinedexpath
    中不要执行异步调用-在加载视图时从外部获取数据,然后触发重新加载tableView以访问当前加载的数据。@luk2302建议在哪里添加数据?因为我已经有了一个loadData()函数,它在其中获取PFObject并将其添加到PFObjects的NSMutableArray中。您在
    viewdiload
    中获取数据,当数据到达时,解析它,存储它并告诉tableView重新加载。