[英]Refresh UITableview without scrolling
我從API獲取了所有數據,並將其按10個項目添加到tableView
。
當我調用reloadData
方法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()
}
}
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.