Paging and UITableView? - iphone

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.

Related

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.

UITableViewController is not updating content is I apply manual sorting

using a UITableViewController, where I am displaying some products which are sorted based on make year. Whenever user taps in a row (each row represents a single product) a new view gets displayed to edit the same product.
So after save if the user has changed the make year then it should display in main table view at appropriate place (i.e. sorted again accordingly).
To accomplish this I am calling sort() method in.... here sort method returns NSMutableArray type of object.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Sort the records based on number of days.
sortedProducts = [DTOConverter sort:unsortedProducts];
return [sortedProducts count];
}
But the problem is that in table view I am still having the old values in that particular row until I bounce the entire view then it gets populated with new updated values.
Same thing happens in case of a new product gets added.
Please correct me where I am doing wrong, I am sure that the issue might be related to indexPath, but i do not know how to tackle it. Please help.Thanks.
To update tableview data call
[tableView reloadData];
You have to refresh the table or the individual row with one of the following methods:
reloadData
reloadRowsAtIndexPaths:withRowAnimation
reloadSections:withRowAnimation
reloadSectionIndexTitles
See the documentation on each method: UITableView Class Reference
Note: if you are only updating one row, do not call [tableView reloadData]. It is not necessary to reload all of the data in the table.
You may have to set up a delegate in the new view you are loading from your tableview and then set up your tableviewcontroller to implement the delegate method of that new viewcontroller. Then call reload data from that delegate method Here is an example of a delegate: Delegate example

Marking an item as newly added in TableView

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.

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.

Scroll UITableView row to top, even when table contains only a few rows

I have a UITableView which sometimes has only a few rows. I would like a specific row, e.g. the 2nd out of 3, to appear at the top of the table. However, when the number of rows do not fill the entire table, calling the following has no effect:
[TableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfTopItem inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]
Is there a way I can force a row to appear at the top even with a limited number of rows in the table? Thanks
The only way to achieve this is to set the table's contentOffset. This will ensure the desired row appears at the top of the table, even when the rows do not fill up the entire table.
I'm not sure I understand exactly what you want but if you want to place a particular cell at the top of the table why not rearrange the data instead of the views?
Assuming your cell (row) data is not hard coded you can maybe do something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0){
// Load the data you want to appear at the top
}else {
// Load the rest of the data
}
}
You have to manually keep track of the top row data however so this might not be the best solution.
Alternatively if your data is in an array, you can swap the element you want on top with the first element in the array then call [myTableView reloadData] to reload the data. In this case you don't have to change cellForRowAtIndexPath.
Of course this is not very efficient since you're manipulating arrays and reloading your table but if your table only has a few rows and you don't have to do this swapping often it might be acceptable.
Rearrange the order, or have a button that shows the previous dates. You can use the numberOfRowsInSection method to check a BOOL, and then set the right number of rows. Then in the cellForRowAtIndexPath method, you again check the BOOL, and return the valid cells. On the button press, set the BOOL, and call the tableView's reloadData method.