sectionForSectionIndexTitle in UITableView iOS - ios

I have just started in ios, and I am stuck with UITableView.
I want to implement index bar in right side of the view. I have implemented this but when I tap on the section on the index bar it's not working.
This is my code. Here indexArray is as "A-Z" element and finalArray is my UITableView mutable array.
Please tell me what should i implement in sectionForSectionIndexTitle.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [finalArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return [finalArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return indexArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle: (NSString *)title atIndex:(NSInteger)index
{
}

This is perhaps one of the most confusing methods in the UITableViewDataSource, its best to consider an example:
Lets say you have a table view with 4 items:
section index - 0 section title - 'A'
items: ['Apple', 'Ant']
section index - 1 section title - 'C'
items: ['Car', 'Cat']
You then have 26 section index titles (the index on the right side of the table view): A - Z
Now the user taps on letter 'C' you get a call to
tableView:sectionForSectionIndexTitle:#"C" atIndex:2
You need to return 1 - the section index for the 'C' section.
If the user taps 'Z' you will also have to return 1 since you only have 2 sections and C is the closest to Z.
In the simple case where you have the same sections in your table view as the index on the right had side its ok to do this:
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
return index;
}
Otherwise it depends on your setup. You might have to look for the title in you section titles:
NSInteger section = [_sectionTitles indexOfObject:title];
if (section == NSNotFound) // Handle missing section (e.g. return the nearest section to it?)

Related

Index Section in viewForHeaderInSection

i have UITableView header custom like Instagram where my section acts header, in this tableview i also using paging to get the data from server.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (currentPage == 0) {
return 1;
}
if (currentPage < totalPages) {
return myObject.count+1;
}
return myObject.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section
{
myTimeline = [myObject objectAtIndex:section];
//the rest of my code to display text in UILabel cell, etc
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.tag == kLoadingCellTag) {
currentPage++;
[self getTimeline];
}
}
Now the problem is, I already make sure that my paging is working and myObject array count is increase.
But when i do the scroll it crash in method viewForHeaderInSection with reason "index 10 beyond bounds [0 .. 9]'" (i set the paging in in server with 10 items per page)
I think the section in ViewForHeaderInSection not updating with numberOfSectionsInTableView method that i already setup.
So how to make the section in ViewForHeaderInSection updated?
Thanks..
Your numberOfSectionsInTableview has return myObject.count+1;, so the table view is dutifully asking for the header for the last section, which is beyond the bounds of your myObject array. You need either to remove the +1 or, if it's there for a reason, to include code in viewForHeaderInSection to handle that special case:
if (currentPage > 0 && currentPage < totalPages && section == myObject.count) {
// create whatever header view is appropriate for the final section
} else {
myTimeline = ... etc
}

UITableView tapping index doesnt scroll to section

I have a tableView where I have implemented section headers and I have the section titles appearing along the right hand side. However, when I tap the index it doesn't scroll to that section even though I can see in my logs that it is returning the correct section.
Here is my method for tableView:sectionForSectionIndexTitles:atIndex
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSLog(#"Title: %#", title);
NSLog(#"index: %d", index);
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Any help would be great, thanks.
If you want to create alphabets scroll. This will helps.
-(void)viewDidLoad {
//section index in tableview
alphabetsArray = [[NSArray alloc] initWithObjects:#"#",#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}

Implement 'search' OR sort in ios

I get list of names from web service in table view. My requirement is that I need to display the names starting from alphabet A / B / C and so on in iOS. Please refer image (red rectangle) below. Any suggestions how to implement this?
Take a look at the UITableViewDataSource methods
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
you need to return an array with the titles for the various sections that will appear on the right. For instance:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return #[#"A",#"B",#"C"];
}
and
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
return the index of the section to scroll to once the index on the right is tapped. You have to handle with sorting yourself, all the indexes do is scrolling to a certain section when an index on the right is tapped.
Your friend here is UILocalizedIndexedCollation. There's an explanation of how to use it at NSHipster.

number of rows in section

I have a table view which populates from a plist. in a plist I have a Categories array:
Catgeories - Array
item 0 - Maths
item 1 - English
....
in the same plist i have a Notes array
Notes - Array
item 0 - Dictionary
Title - My maths note 1
Text - This is algebra
Category - Maths
item 1 - Dictionary
Title - My English
Text - This is algebra
Category - English
in number of sections i have...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.categories count];
}
In numberOfRowsInSection before i added the categories array to the plist i could simply use
return self.notes.count;
which produces a duplicate in every category: http://i.stack.imgur.com/9xScH.png
but now in this method i should expect to return the number of rows based on the section, not sure how to do this
You need to do something like that:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return self.notes.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.notes[section].count;
}
You get array of you notes base on the section and return count of the array.
//EXTENDED
If you want to have as many section as data in Category array use:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return self.catgeories.count;
}
The self.note array contain dictionaries object with Category key and I believe you want to show all of the data for section where category is equal section category. To do that you have to filter your notes array, try this (I assume that you have distinct objects in Caregory plist):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Get category
NSString *cat = self.catgeories[section];
//Filter notes array
NSPredicate *pred = [NSPredicate predicateWithFormat:#"Category =[cd] %#", cat];
NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred];
return catArray.count;
}
I have edited the answer.
If you wanna access large amount of data, i suggest that you store your data in core data instead of plist,
For getting number of rows for each section you can use this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// check if the count of the array is greater than number of sections
if (self.notes.count > section)
return [[[self.notes objectAtIndex:section]allObjects]count];
}
Hope it helps :)
You can try like this if you want to return different number of rows in each section:-
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:
(NSInteger)section
{
int rows=0;
if(section==0)
{
rows=1;
}
if(section==1)
{
rows=2;
}
rerurn rows;
}

UITableView Section with Index

Anything I missed in the following code? I can create a list but the list is array x26 times (26x section) in the UITableView. A list shown, but the section title is only point to 'A'
arr_indexpeople > An array of A,B,C,D,E.....Z
arr_completeArray > An array of a list of people
Thank you.
return [arr_indexpeople objectAtIndex:section]
You are returning objectAtIndex 0 in titleForHeaderInSection. Change it to-
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}
The datasource method will get called for every section to load, so give the dynamic index of a section as input variable for arr_indexpeople as shown below.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}

Resources