iOS: Horizontal UITableView inside another UITableView issues - iphone

I want to create a table with horizontal scrolled rows. I have followed Felipe Laso's tutorial in Raywenderlich's blog, here. It works perfectly except for 2 issues.
First, the inner horizontal table should have a dynamic number of cells based on an array fetched asynchronously using ASIHTTPRequest block from the outer vertical table.
//Inner horizontal table number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//self.articles is being assigned from cell.articles asynchronously from the outer vertical table
return [self.articles count];
}
The code above gives me an error, if the self.articles hasn't yet been assigned. So how could I determine the number of rows from the outer ASIHTTPRequest block and reload the inner table?
Second, I want to push another view based on each cell's horizontal and vertical indexes.
I have 2 didSelectRowAtIndexPath method in the 2 table, I tried to NSLog the indexpath.row at both tables, the inner table logged the cell's index, however the outer table logged NULL.
So what should I do?

If you haven't read your data in yet, then you don't have any rows, you should return 0. When you have read in your data, then when the read is complete, call reloadData on the list to refresh all the calls. You probably want to push your second view at this time too.

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.

UITableView - Adding additional rows

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];

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.

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.