Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

ios - How to manually call "tableView numberOfRowsInSection" function

I'm making an app that fetches data from CloudKit and displays it in a tableview. At the end of my viewDidLoad, I call a different function to fetch this data and filter it. However, my tableView numberOfRowsInSection function seems to get called before my fetching function, and so I get no rows in my tableView since hasn't gone through my fetching function yet, and a view I have assigned as a background view pops up, as it should when I have no rows. I'd like to know, is there a way to call my tableView numberOfRowsInSection function again at the end of my custom function, so that it can update the tableView? I know how to call a normal function:

function(any parameters here)

I can't seem to figure out how to call this, though:

tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

Assume I can't move my fetching function code into ViewDidLoad.

question from:https://stackoverflow.com/questions/65909826/how-to-manually-call-tableview-numberofrowsinsection-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't call numberOfRowsInSection directly. Just call reloadData() on your tableView like this:

yourAsynchronousFunctionThatGetsYourDataFromCloudKit(){ records in
  //Set your data source with the CKRecords you got from CloudKit
  self.items = records

  //Reload the table
  self.tableView.reloadData()
}

//This gets called by reloadData()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return items.count
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...