刷新uitableview而不滚动



我从API中获取了所有数据,我将其添加到tableView中的10个项目。

当我调用方法reloadData时,tableView对不同项目进行了巨大的滚动。

这是我的代码

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

新出版物和NomorePublications方法

func newPublication() {
   downloadingPost = false
}
func noMorePublications(){
   spinner.stopAnimating()
}

tableview dataSource方法:

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
}

我解决了问题。

我必须在表的代表

的代表处添加估计值
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
   return HeightOffset
}

这有助于在加载这些细胞之前了解单元的大小。

最新更新