reload cell data in table view with Swift - ios

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!

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

To reload visible cells of tableview in Swift 3.0
pTableView.beginUpdates()
pTableView.reloadRows(at: pMessagesTableView.indexPathsForVisibleRows!, with: .none)
pTableView.endUpdates()

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

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

Related

Without reloading Tableview, update the changed index cell values using Observer in iOS Swift

In the first time I collect the array values from API response and displayed it in tableview, again I will get the same response from socket and reload the values in table, But here I don't need to reload entire table, I want update the cell's which value has been changed.
Here Compare the two array's, from which index has changes, just need to update that index row cells only, without reload entire table view.old and new array, CodeSample
But you should be careful if will change some indexPaths in you data stack, if it gonna happen - use tableView.deleteRows or tableView.deleteSections. This updates should be qual in table and in dataStack or crash
let indexPaths = [IndexPath]()
tableView.performBatchUpdates {
self.tableView.reloadRows(at: indexPaths, with: .none)
}
or
tableView.beginUpdates()
tableView.reloadRows(at: indexPaths, with: .none)
tableView.endUpdates()
btw, you can make your indexPaths to update like let indexPaths = tableView.indexPathsForVisibleRows - method's name is speechful, in case if you have socket I suppose it would be helpful since u've got dynamic

Update specific cells

So i have a tableview which is showing json data.
first from JSON1 = [{"serverid":65,"name":"Apple"},{"serverid":98,"name":"Mac"}]
and details of each cell fetched from the second json.
JSON2 = [{"serverid":98,"updated": "10:29 PM"},{"serverid":65,"updated": "10:29 PM"}]
now After 60 second i again make a call to servers to get just JSON 2 back (order in which i get my json is random).
there is a parameter last updated. Based on that i will check which cell to update.
But how do I update only selected cells?
(not complete tableview but specific cell)
I know the fucntion "reloadRows(at: [indexPath]" but i don't how to pass selected cell to this function?
When you say selected, that implies the cell has isSelected == true. If that is the case, then you could use indexPathsForSelectedRows
if let indexPaths = tableView.indexPathsForSelectedRows {
tableView.reloadRows(at: indexPaths, with: .automatic)
}
However, if by selected you mean rows in your data source that need to be changed by the 60 second update, then you need to provide more details. How are you binding the data source to cells tableView(_:cellForRowAt:).

Lazy loading API requests in UITableview using Alamofire

I'm new to swift and iOS Development. I want have 100 cells in my UITableView and each cell has a different API request that returns JSON data which I will save to the CoreData and then display in the UITableView. How do i handle this? I want to implement lazy loading but i have no idea how to do it in this case. Please point me to the right direction.
You Can loading table view in main_queue
//Use swift 2.3
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.yourTableView.reloadData()
})
//Use swift 3
DispatchQueue.main.async{
self.yourTableView.reloadData()
}

Reloading data from a tableView within a collectionViewCell

I have a tableView inside a collectionViewCell and I get an error when I try to reload the data.
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:1610
I tried using Dispatch.main.async and it seems to get rid of the problem. The only thing is that it doesn't reload the data and nothing changes in the tableView
func cleanItems(completion: #escaping (Bool) -> Void) {
Dispatch.main.async {
cell.tableView.beginUpdates()
// Make changes in the Data Source
cell.tableView.deleteRows(at: selectedItems, with: .fade)
cell.tableView.endUpdates()
// Reloading sections accordingly depending on what the user has deleted
// Do I need to reload data here again? It used to work without Dispatch, but it wasn't stable
cell.tableView.reloadData()
// Updating items with if statements to reload data in Firebase
completion(true)
}
}
This doesn't reload the data at all and nothing seems to change. The good thing is that I don't get a random crash, which was the case before implementing Dispatch.main.async
I've retrieved the numberOfRows in each section to see how many rows there are after ending updates.
print(cell.tableView.numberOfRows(inSection: 1))
and I get the same number of rows that are in the current view.
This is crucial, because if the tableView sections are all zero, the collectionViewCell should disappear. And we never get here in the completion block as it says that the numberOfRows has never changed. Leaving us with a non updated tableView.
I solved this by moving Dispatch.main.async outside the function call.
Dispatch.main.async {
cleanItems(completion: { (success) in
etc.
})
}

How to update, not reload, UITableView?

My question may be looks simple, but it doesn't work. I searched a lot and try a lot of methods, but it doesn't want work!
So, I have an UITableView. In the background one of my functions work and detect, if a new message has been received, it writes this value into Core Data and I want to add badges on my cells when I receive a new message.
But when I try this:
func reloadTableView() {
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
})
}
it doesn't update my tableView and doesn't show new badges. For this, I need:
Change my controller and return to it
Or I need drag my tableView to top and leave it
in these cases it will show my badges.
So, how can I update my tableView to show new added badges without described above 2 methods?
UPDATE
UPDATE 2
I have 2 files: ContactsTableViewController and Messages.swift.
In my Messages.swift I handle new messages and when I receive a new message I get in logs:
print("New message for \(user.jidStr)")
ContactsTableViewController().reloadTableView()
In my ContactsTableViewController:
func reloadTableView() {
self.tableView.reloadData()
print("updated")
}
It reloads my tableView, as I get updated message in logs, but it doesn't updated it and my badges
Try to update only cells that receive new message with:
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: section)], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
where row - the row of your cell in it section that receive the message
Example in Objective-C:
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
Example in Swift 2.0:
self.tableView.reloadRowsAtIndexPaths([NSIndexSet, indexSetWithIndex:0], withRowAnimation: UITableViewRowAnimation.Automatic)
Did you try the NSOperationQeue?
func reloadTableView() {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.tableView.reloadData()
})
}
Edit (because I can't comment)
override func viewDidLoad() { NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "reloadTableView", userInfo: nil, repeats: true) }
If you are using something like this you call the update function every second and this doesn't make sense. You should call it after getting your data
The reloadData should work... ( I think it is better to call it on the main thread. )
Have you checked that your objects array is correct ? If it comes from a CoreData request, it may use the cache. Be sure that you have synchronized the CoreDataContext ( by calling the save method on the context after your insertion ) and that your fetch returns the last created objects.
Just a guess…

Resources