简体   繁体   中英

Refresh UITableview without scrolling

I got all the data from the API and I'm adding it to the tableView by 10 items.

When I call the method reloadData , the tableView makes a huge scroll to different item.

This is my code

func getSystemFeedPublicationsSuccess(httpCode: Int, data: Data) {
   var jsonData = JSON(data)
   let publications = jsonData["content"]["publications"].array
   var indexPath = [IndexPath]()

   for publication in publications! {
       if publication["publication_image"].rawString() == "null" {}
       jsonDataTable.append(publication)

       let indexPathTemp = IndexPath(row: jsonDataTable.count - 1, section: 1)
       indexPath.append(indexPathTemp)
   }

   feedProtocol.tableView.reloadData()

   morePublications = jsonData["content"]["pending"].int!

   if(morePublications == 1) {
       feedProtocol.newPublication()
   } else {
       feedProtocol.noMorePublications()
   }
}

NewPublication and noMorePublications Methods

func newPublication() {

   downloadingPost = false
}
func noMorePublications(){

   spinner.stopAnimating()
}

TableView datasource method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


   if(indexPath.section == 0){

       switch jsonDataProfileArray[indexPath.row]["sort"].int! {
       case 0:
           let cell = tableView.dequeueReusableCell(withIdentifier: "MainInformationEntitiesTableViewCell") as! MainInformationEntitiesTableViewCell

           jsonDataProfileArray[indexPath.row]["height"] = JSON(cell.initView(infoBasic: jsonDataProfileArray[indexPath.row]))

           return cell
       case 1:
           let cell = tableView.dequeueReusableCell(withIdentifier: "ImageGalleryTableViewCell") as! ImageGalleryTableViewCell
           jsonDataProfileArray[indexPath.row]["height"] = JSON(cell.initView(galleryInfo: jsonDataProfileArray[indexPath.row]))
           return cell

       case 2:
           let cell = tableView.dequeueReusableCell(withIdentifier: "DescriptionAccountTableViewCell") as! DescriptionAccountTableViewCell
           jsonDataProfileArray[indexPath.row]["height"] = JSON(cell.initView(companyJson: jsonDataProfileArray[indexPath.row]))
           return cell

       case 3:
           let cell = tableView.dequeueReusableCell(withIdentifier: "suggestAccountTableViewCell") as! suggestAccountTableViewCell
           if((jsonDataProfileArray[indexPath.row]["height"]).int == nil){
               jsonDataProfileArray[indexPath.row]["height"] = JSON(cell.frame.height)  
               cell.initView(companiesArray: jsonDataProfileArray[indexPath.row]["similar_companies"].array!, companiesCount: jsonDataProfileArray[indexPath.row]["nCompanies"].int!)

           }

           return cell

       default:
           print("default")
       }


   }else{

       let cell = tableView.dequeueReusableCell(withIdentifier: "PublicationTemplateTableViewCell") as! PublicationTemplateTableViewCell
       cell.publicationTitleLabel.text = "\(indexPath.row)"
       return cell

   }

   let cell = UITableViewCell()
   return cell
}

I resolved the issue.

I had to add estimateHeightForRowAt at the Delegate of the table

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
   return HeightOffset
}

This help to know the size of the cells before loading these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM