繁体   English   中英

刷新UITableview而无需滚动

[英]Refresh UITableview without scrolling

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

当我调用reloadData方法reloadDatatableView会滚动到另一个项目。

这是我的代码

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和noMorePublications方法

func newPublication() {

   downloadingPost = false
}
func noMorePublications(){

   spinner.stopAnimating()
}

TableView数据源方法:

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
}

我解决了这个问题。

我必须在表的代表处添加estimateHeightForRowAt

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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