UITableView - Adding additional rows - iphone

I have created a UITableView in an app, using the basis of a tutorial from the following link;
http://www.theappcodeblog.com/?p=353
I have customised the style to my needs and it works perfectly. However, if i wanted to add additional rows to the table I come across a problem. I have my three NSMutableArrays which contain category, subCategory and categoryDetails. I have added an additional entry to each of the existing 6 to form a new row.
I assumed that would be ok, as the numberOfRowsInSection code is set to return [category count];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [category count];
}
I assumed that by adding an entry for each array, adding an image it would check the category count and auto add a new row. However, what it actually does it repeat the first array and then stack the new one on top of it overlapping.
Any ideas?

It would seem that its something to do with the cellForRowAtIndexPath. Without actually seeing the code i'd say that you are adding new labels pertaining to the 7th row to the already created 6th row. And hence the overlapping.

Are you calling the reload data method of the table view when your data changes?
[tableView reloadData];

Related

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

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.

Deleting all objects from a section in a Core Data backed UITableView doesn't trigger the selection to delete?

I'm having a problem managing the sections in my Core Data backed UITableView, using an NSFetchedResultsController.
My table view can have 2 sections, the first is a static section and is displayed conditionally. The second section is being populated with the fetched results controller. I'm using the following to determine how many sections to display. If I delete all of the fetched objects in the second section, I would expect that section to be deleted as well.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
NSInteger numberOfSections = 0;
if (self.locationEnabled) numberOfSections++;
if ([[[self.fetchedResultsController sections] lastObject] numberOfObjects]) numberOfSections++;
return numberOfSections;
}
The problem with this approach is that this datasource method is informing the table that it should be displaying 1 section because there are no fetched objects remaining, but removing the last object never triggered a deletion of the section through the fetched results manager delegate methods.
How do I trigger a section deletion to avoid receiving UIKit inconsistency errors?
Edit: I should point out that I have sectionNameKeyPath set to nil since the results don't share anything in common to section by.

objective-c programmatically change table view from standard to grouped?

The following situation pertains to an iPad app I am working on for my company.
So I have an array of type "Person" that holds the information of a list of people. On the initial page, I have a table that displays their name and picture. When this loads, the results are ungrouped. I was wondering if there was a way to easily group these results with the click of a button based on something like location or business title. The reason they are not grouped when they are loaded is because the powers higher then I deemed it necessary to display the raw list first. Any ideas on doing something like that? Thanks in advance!
You'll want to use "sections" to split the table.
First you need to sort your datasource into the desired groups, and then reflect it in the view.
A dictionary can easily represent the grouped data. Model each group as an array that contains the entries for that group, and then add it to the dictionary with a key that represents the section.
When the data is ungrouped, you can still use the dictionary, but with a single entry, rather than many, and then reflect that in the UI.
That way when you're asked how many sections there are in the table you can return [dictionary count]; and then when you're asked for the number of rows, you can do something like [[dictionary objectForKey#"myKey"] count];
And so on.
When you've reconfigured your datasource you can call [self.tableView reloadData]; to reload the table with the sections.
You'll have to remove the UITableView and add another in it's place via [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];. You can't change an existing style at runtime.
The other way to do it, is have it grouped from the get-go, but return 1 for - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; if it is "ungrouped" and return all rows for - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section when it is grouped, do it the normal way.

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.