Marking an item as newly added in TableView - iphone

I have a UITableView where I can add a new item with a touch of a button. I would like to highlight this newly added item. Since UITableView is data-driven and cells are recycled, the only way I can think of so far to mark this newly added item is to add a BOOL flag in the data itself, then highlight the cell and negate the flag on first encounter.
Is there another way of doing this without having to contaminate the data source?

My first thoughts..
Create an Integer [myObjectArray count];
When you add the new object you can reload table
Create a new integer [myObjectArray count];
Compare the two integers when table reloads. If the 1st int is less than the new int
then you obviously have added another object.
When creating cell check if the cell indexpath.row is equal to the last object count in your array, if it is then you can fill the cell background with another color.
Hope that makes sense?

Assuming your data is ordered and indexable, use another NSArray (or NSMutableArray) of NSIndexPath to maintain a list of newly added data. If you only have one section, then you could substitute NSNumber for NSIndexPath and just record the rows.

Related

Paging and UITableView?

Is possible to add a pagination for UITableView rows?When the user scrolls vertically the UITableView it change rows!
You can use the method tableView:willDisplayCell:forRowAtIndexPath: for adding more rows at the bottom. This method is called every time before a row is about to be displayed. In the method, check if the row is corresponding to the last item in your data source. If yes, add more rows at the end.
if(indexPath.row == [self.items count] - 1) //items is your data source
{
[self addMoreItems];
}
Making pages in a tableView is not hard, it's just a matter of allowing the table to expand when required.
Use a mutable structure, such as a NSMutableArray as your main source of data for the tableView, when the user clicks on the last row (or the row loads, however you want). Get the new data, append it to the array, and then force the tableview to reload.

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

How can I return the count of objects for an FRC fetch?

I am trying to use the count of objects from my FRC fetch so that I can create an "Add" cell after all of my fetched objects have been listed in a UITableView. Is there a method for returning the number of objects fetched? I can only find a method that returns the count of sections for a given FRC.
Simplistically, you You want a count of fetchedObjects. So:
[[myFRC fetchedObjects] count];
The problem that you're going to have with adding an additional cell, is that every time the table ask for the count of sections and rows, you're going to have add 1 to the count so the table knows to add the extra row.
Bjarne Mogstad is correct. It's quicker and cleaner to put the add element in a header or footer for the table itself. That way, the table directly and cleanly represents the data structure without having to constantly adjust for the phantom row.
The easiest way to do this is by setting the tableFooterView property of your table view. The footer view will be displayed below the last table view cell.

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.

how to get dynamic values from uitableview cells?

If i have to show 10 or 30 or 100 strings in a uitableview..How to get the selected raw from that.. My value for each row is different .I mean i have to set the row value and display name from a 2 dimenional NSArray.. Somebody please help me..
I would setup an NSMutableArray of the objects your table rows represent. You can use your own class or NSDictionaries, one per row. My data source implementation (UITableViewDataSource protocol) of -tableView:cellForRowAtIndexPath: would set each cell's content (be it strings, images or anything else) to the appropriate object of the NSMutableArray. My delegate implementation (UITableViewDelegate protocol) of -tableView: didSelectRowAtIndexPath: would be called whenever a row is selected and the supplied NSIndexPath would point to the corresponding object's index in the NSMutableArray.
Basically, just implement UITableViewController which is by default both the data source and the delegate of the table view, put the objects you need to "show" in the table into an NSMutableArray and implement -tableView:numberOfRowsInSection:, -tableView:cellForRowAtIndexPath: and -tableView: didSelectRowAtIndexPath: in the most straight-forward manner. The first method returns the number of rows per section, the second configures a cell for each row, the third is sent when a row is selected.