19

how is it possible to reload data for a cell in my table view? I want to do a request to my Server and the server responses JSON. At the moment I implemented the request and the response handling and I can show the data in my cells. But I want that the server only responses maybe ten datasets. When I scroll to the end of the table view with my ten cells in the app I want to do the next request to my server and the server delivers the next ten datasets and so on. I work with Swift as the programming language.

THX!

GRme
  • 2,707
  • 5
  • 26
  • 49
  • you can get Array of visible Cell by using TableView Function tableView.visibleRows() or You Can Get IndexPath of Visible Rows By tableView.indexPathsForVisibleRows() ! and then you can reload table by tableView.reloadData() Function! – Saurabh Prajapati Nov 03 '14 at 08:30
  • look at this example https://github.com/Haneke/HanekeSwift – Saurabh Prajapati Nov 03 '14 at 08:32

4 Answers4

51

You can use self.tableView.reloadData() in order to reload your entire table or self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.None) in order to reload a specific cell.

You can read more about the UITableView class here

YogevSitton
  • 10,068
  • 11
  • 62
  • 95
  • 8
    yes - that's why the code I gave is in swift... I'll add the "self." just to make sure. – YogevSitton Nov 03 '14 at 07:31
  • 1
    I too use `self` to indicate it's an ivar ;) for me that makes it much more clear. – HAS Nov 03 '14 at 08:32
  • @YogevSitton self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.None)...I have tableView from which I want to add a new cell at 0 index....How can I do that, I mean..I not getting how to create a indexpath object with specific row and column, as there is no constructor of that type? – Maninder Singh Jul 09 '17 at 23:23
12

To reload visible cells of tableview in Swift 3.0

pTableView.beginUpdates()

pTableView.reloadRows(at: pMessagesTableView.indexPathsForVisibleRows!, with: .none)

pTableView.endUpdates()
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Usman Nisar
  • 3,031
  • 33
  • 41
9

Try this...

Swift 2.0

dispatch_async(dispatch_get_main_queue(), { () -> Void in
   self.tableView.reloadData()
})

Swift 3.0

DispatchQueue.main.async {
    self.tableview.reloadData()
}
Lucy Jeong
  • 320
  • 7
  • 21
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • If you're calling reloadData() inside of a block, you have to dispatch it so that it's on the main_queue – NSCoder Mar 29 '16 at 10:50
4

you can get Array of visible Cell by using TableView Function tableView.visibleRows() or You Can Get IndexPath of Visible Rows By tableView.indexPathsForVisibleRows() ! and then you can reload table by tableView.reloadData() Function!

look at following links

link1

you can read about UITableView Class Here

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Saurabh Prajapati
  • 2,348
  • 1
  • 24
  • 42