Update tableView with AccessoryCheckmark when switching back to that TabView - iphone

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".

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.

Update table cell on notification

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.

NSTableView multiple Selections

I have an NSTableView with multiple selections enabled
If I select items and scroll the list back and forth they are remember and shown and selected (blue background).
Using the didDeselectRowAtIndexPath and didSelectRowAtIndexPath I am able to keep my own array of selected items.
that part works well.
However If I then use the sectionForSectionIndexTitle and jump to a letter The selection appears to be forgotten even indexPathForSelectedRows appears to have been reset and is now empty.
My own array remembers that an item is selected and I can set the cell.selected in the cellForRowAtIndexPath but the instant I move the list it is forgotten again
Any ideas is this a bug or how do you retain the selection list when jumping to a letter
Thanks in advance
Remember the UITableViewCells are there to display your data only and by no means they have a 1-to-1 relationship with your "model" as the are queued and reused when necessary (i.e. you may have thousands of items on display in a tableview but only a handful of instances of UITableViewCell allocated).
So you need to keep track of the selected / deselected items in a mutable array and make sure that you check for the presence of that item in your tableView:cellForRowAtIndexPath: method and then update the cell properties to represent your data.
By using an NSMutableArray of selected row values, it may be possible to keep track of the selected rows in the table yourself. Then it would be a matter of calling -selectRowAtIndexPath:animated on the last object in the array to restore the selections.
As for the bug: what could possibly be happening when the table jumps to a selected letter, is a stray -reloadData which would forget the selected row(s).
I think it is happening because your cells are reused. Do not reuse the cells(you are not using your cells efficiently). otherwise,
You can store the selection by yourself in an array and use it later for your purpose.!

Refreshing/releasing 2nd tableview from previous query in drill down tableview

I have a drill down tableview that is using sqlite.
The first one is a tableview where i load some categories using a SQL query.
When i click a row it's using the following query to show the appropriate records for the category:
#"SELECT * FROM table WHERE category IS ('%#')", sharedSingleton.category];
This works.
When i click the back button on tableview 2 and return to the category tableview and then touch another category tableview2 still shows the rows for the category i selected the first time.
I'm new to this stuff but i searched a long time and tried releasing objects or set value's to nil but is doesn't seem to help.
Am i in the right direction, how can i refresh or release tableview2?
Thanks in advance.
Yes you are in the right direction.
When selecting a row table 1 you might be calling a method which loads the second table view and assigning the data to be loaded to the corresponding table view,Which is fetched and filled when delegate/ data source methods of table view is called.
You might have created tableview as a different view controller class, which is initialized and data is reloaded for the first time. But even though you are fetching the second set of data its not getting reloaded automatically even after you are assigning it.
After assigning the data you have fetched into the obect which distributes it to tableview Reload the table data once you assign it to. you could call the one line of code to do this
[tableview reloadData];
Regards,
Jackson Sunny Rodrigues

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.