UITableView - implementing index - iphone

I want to implement an index that is being displayed at the right side of a table. I've found out how to display it.
How can I scroll to a certain position in my table, after a certain index item was pressed?
In the index I am not displaying all the alphabet, but in the following manner:
#define ALPHA_ARRAY= #"A●D●G●J●M●P●S●V●Z"
So when the user presses between A and D I want to go to items starting from A and so on. .
How can I implement it?
I want to make something similar to the index available in the contacts application on iPhone.

You need to implement this method in your table view data source:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
The table view gives you the index of the section the user touched, along with its title (in this case, one of the letters you specified). You need to return the index of the section. If there are exactly as many sections as there are indexes, all you have to do is return index. Otherwise, you'll need to figure out the index of the section the table view should scroll to and return that instead.

Related

iOS: Horizontal UITableView inside another UITableView issues

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.

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.

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.