Update table cell on notification - iphone

How can I update a tabelview cell when a particular notification received, without reloading table data. notification tells the id of the cell that I need to update. So I want to iterate all table cells and update only the relevant table cell without reloading it. how can i do this

Use [tableView visibleCells] to get a list of all cells that are displayed. Iterate through this array to see if the cell you need to update is displayed, then do whatever you need to. (this method is if you're reusing cells).

You can just use the built in reloadRowsAtIndexPaths:withRowAnimation: instead of trying to usurp the framework's functions. This will efficiently only reload the cell you ask it two not the entire table or even an entire section.
Reloads the specified rows using a certain animation effect.
Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it
animates the old row out. Call this method if you want to alert the
user that the value of a cell is changing. If, however, notifying the
user is not important—that is, you just want to change the value that
a cell is displaying—you can get the cell for a particular row and set
its new value.
When this method is called in an animation block defined by the beginUpdates and endUpdates methods, it behaves similarly to
deleteRowsAtIndexPaths:withRowAnimation:. The indexes that UITableView
passes to the method are specified in the state of the table view
prior to any updates. This happens regardless of ordering of the
insertion, deletion, and reloading method calls within the animation
block.
Available in iOS 3.0 and later.

Related

How to delete tableviewcell automatically

I have an array like [1...100], and I display it in a UITableView. When I add a cell to that tableview, I want the cell before it to be deleted automatically. Is this possible? I tried many solutions, and I know how to delete a cell by using the commit editing style method, but the the cell needs be deleted automatically, not with user input.
You would have to change your model and then reload the table. Let's say, as per your example, that your table contains rows with 1 to 100 in them. Your model would look like this:
var model = [1..<100]
And in cellForRowAtIndexPath you would set the text of every cell to an item in the array. Therefore, if you update the model and reload the table view, that will "automatically" delete cells. Your adding function, which deletes the last cell before adding a new one, might look like this:
func addItem(n: Int) {
model.removeLast()
model.append(n)
tableView.reloadData()
}
This allows you to manipulate the model however you want, and then update your table without having to worry about working with removing and updating cells.

UITableViewCell is frozen (not rerendering with new data). How to fix?

Having some issues with a tableViewCell freezing. I have a table view cell with a list of exercises. When I add a new exercise on another viewController, then pop back to this tableViewController that lists all the exercises, the respective cell gets frozen (see screenshot). Then, if I call [self.tableCell reloadData], that cell never refreshes.
it's weird because if the offending cell is, say, index #4, and I change the data source to only have 2 items, the 4th cell is still frozen. (the first 2 render correctly, the 3rd is blank, 4th is offending cell)
List of all exercises:
When I change the data in the tableView data source, that cell is still frozen:
If I put a breakpoint and inspect the tableView, it doesn't say anything about that frozen cell:
Not sure where to go from here :( It seems to only happen when I insert a row from a different viewController then pop back to this one.
Any thoughts? Thanks so much for any help!
You are keeping a reference to a cell. That is a bad idea. It contradicts the whole pattern of datasource and recycling of table cells. All kinds of wacky bugs will be the inevitable result.
Instead, you should make sure your datasource methods return the correct number of sections and rows, and the appropriate cells and then reload your table not a cell.
[self.tableView reloadData];
So, for example, when the view is returning from adding a new item, make sure the data array you are using is updated and reload.
I exactly don't understand your way of managing tableview along with data source. If you make any change in data source, same changes should be reflected in tableview as well. (Both should be synchronized). You can use:
Table reloadRowsAtIndexPaths:
Table deleteRowsAtIndexPaths:
Table insertRowsAtIndexPaths:
to make changes to your tableview and its cells. Or you can just call [Table reloadData]
Remember: never try to store references of UITableViewCells as they are dequeued again on screen - your reference will be just a trash. Just alter your cells using above 3 methods and you are good to go. If you want to clear something, just ask for it.

Editing controls disappear on reload

Having a bit of trouble.
I've got a UITableView in grouped mode. The table is set to always be in editing mode - which is working just fine. All my rows are delete-able. The rows are indented and all of my UITableViewCellEditControls show up great.
Problem is when a row actually gets deleted I update my data source and then call:
deleteRowsAtIndexPaths:
reloadRowsAtIndexPaths: //needs to get called because of table aesthetics
After the reload occurs (I've tried just a simple reloadData, too) all of my UITableViewCellEditControls (the red circles with the minus signs etc) disappear! The rows are still indented, but they're gone.
I've tried the suggestion on this post:
UITableViewController canceling Edit mode animation when calling [table reloadData] inside (void)setEditing
But no luck.
Any help would be REALLY appreciated!
Without looking at any code, I have some suggestions.
According to Apple's UITableView Class Reference: under ReloadData, it says:
Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view's delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates
Blockquote
it shouldn't be called when inserting and deleting rows.
From what it seems like, you are allowing them to delete individual cells, therefore, I am assuming the following methods are already implemented: tableView:editingStyleForRowAtIndexPath: and tableView:commitEditingStyle:forRowAtIndexPath: In your commitEditingStyle, you should be just calling the delete method with something like this:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
This should remove the need for any reload data. If you don't want to show any animation, you can just use noanimation enumerator instead of the fade I showed in example.
If however, you still think you need to do reload data after every cell deletion, maybe try it with performSelector:afterDelay:
Hopefully, this tid-bit helps!

Update tableView with AccessoryCheckmark when switching back to that TabView

My app has 3 tabs, and the array that populates a tableView in third tab can be changed in the other two tabs. When I switch to the third tab, I need the tableView to be updated. I know how to do this, the only problem is that the table view is a check list.
When I tick some of the rows in the third tab, switch to another tab and update the 3rd tab's array, when I want to switch back to the third tab the table is updated with the new data but the checkmarks are removed from the table.
Is there a way to update the table without removing the checkmarks?
How are you determining whether an item is checked right now? It sounds like you may not be storing the checked status of the object, and instead just setting the accessoryType on the cell to UITableViewCellAccessoryCheckmark. If so you need to add a boolean property to the objects stored in the array that will represent whether that item is currently checked or not.
Once you have this value stored you can use it when configuring your tableview cells(either in tableView: cellForRowAtIndexPath: or configureCell: atIndexPath: or wherever else you do cell configuration) to set the appropriate accessoryType for the cell.. Putting this in your cell configuration will get the tableview to correctly check items when reloading the tableview.
Are you reloading the tableview when you tap on 3rd button or creating new tableView.
If you are reloading, Use temp variable to store the data of Checkmarked cell. On every reload compare the temp data with data of cell .If it matchs place checkmark.
when you are adding objects to the array then there you can add a dictionary object, with an extra object #"FALSE",#"checkState". Initially it will FALSE for all objects, so when you are creating cells for table in your third tab check for that key in array. And when you select a cell then access the dictionary object for that indexPath.row in array and update its value to TRUE for key #"checkState".

Is it possible to preload all the cells in a uitableview?

Is there a simple way to preload all the cells in a uitableview?
I need to do this in order to change whether or not all the cells are checked. If I just use cellForRowAtIndexPath, and the user say unchecks all the cells, and then checks a visible cell and starts to scroll again, either the selected cell gets deselected or the newly loading cells are selected.
It seems the easiest way to go would be to preload all the cells if possible.
Thanks!
Don't use the cells as your database.
Your cells are just a narrow window onto your data. The cells just show you a few of the objects in the underlying data. If you try to preload all the cells so you could then select them all, the UITableView could die a slow death, or slow crawl. Especially if we're talking hundreds of entries.
If you want to select all the items in your data, you do so with a direct call to your data to select its objects. Then, you reload the data into your TableView with a reloadData and if everything is set up right, your cells will show the selected state.
Read up on UITableView. Look at Apple's samples. You need to see the separation of data from the view and the controller.
Please re-read the answer I wrote here to your previous, similar question, which explains one solution to your problem.
Again, you should consider keeping an array of on/off settings. You can use NSMutableArray or a C array of int or BOOL values, whatever you want. But it definitely sounds like you need a data model.
Your -tableView:cellForRowAtIndexPath: looks up values of this array. Each value in the array corresponds in some way to a row in the table.
If you only have one section, then you can simply use the ith element of the array to set the checked state of the ith row of the table view. If you use NSMutableArray to store NSNumbers, you can handle resizing quite easily.
If you have more than one section, keep an array of arrays. Each top-level array corresponds to a section. Each inner array corresponds to a section's rows.
If you use NSMutableArray to store NSMutableArrays of NSNumbers, you can handle resizing and section addition and deletion quite easily.
The method -tableView:cellForRowAtIndexPath: then sets up cells with or without checkmarks, depending on the array's value.
Having a data model gives you the freedom to programmatically perform "select all" and "deselect all" button operations.
For example, when you click a button to "select all" cells:
Loop through the array and set each value to YES or 1 or whatever "on" label you chose originally.
Call [tableView reloadData], which is a method that goes back to -tableView:cellForRowAtIndexPath: and which will set a cell's checkmark state based on the state of values in the updated array.
No, you can't do this. You seem to be under the impression that a cell represents a particular piece of data, it doesn't. When the top cell scrolls off the screen it is usually recycled and brought in as the bottom cell. So a list that has hundreds of items you can scolled through may only ever have 8 or 9 cells alloc'ed and initialized.
You need to rethink your application's architecture. When you "uncheck all" it shouldn't change the visual state of the cell, it should change some state in the objects the cell represents, then when you load the cell for the object at that index path you should read that state and set the check mark appropriately.
The changes in the visual state of your cell should always be in response to changes in your underlying model.