I added a table to my app, and with that table, I added a scroll bar, just like the contancts in the iPhone. The scroll is working with the letters, but i have a problem with it, under letter A, i get letter A names, but in letter B I'm getting the same names of letter A. The same words in the .plist of the array of list A is repeating in all the rest of the lists. What can I do??
Note: add your own .plist file to see what outcome it gives.
#import "SimpleTableViewController.h"
@implementation SimpleTableViewController
@synthesize exercises;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(searching)
return nil;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"E"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"I"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"N"];
[tempArray addObject:@"O"];
[tempArray addObject:@"P"];
[tempArray addObject:@"Q"];
[tempArray addObject:@"R"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
[tempArray addObject:@"W"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Y"];
[tempArray addObject:@"Z"];
return tempArray;
}
- (void)viewDidLoad {
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];
exercises = [[NSArray alloc]initWithContentsOfFile:myFile];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section ==0) return 5;
if (section ==1) return 2;
if (section ==2) return 5;
if (section ==3) return 5;
if (section ==4) return 6;
if (section ==5) return 5;
if (section ==6) return 5;
if (section ==7) return 5;
if (section ==8) return 5;
if (section ==9) return 5;
if (section ==10) return 5;
if (section ==11) return 5;
if (section ==12) return 5;
if (section ==13) return 5;
if (section ==14) return 5;
if (section ==15) return 5;
if (section ==16) return 5;
if (section ==17) return 5;
if (section ==18) return 5;
if (section ==19) return 5;
if (section ==20) return 5;
if (section ==21) return 5;
if (section ==22) return 5;
if (section ==23) return 5;
if (section ==24) return 5;
if (section ==25) return 5;
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell=[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle/*Default*/// change default to subtitle
reuseIdentifier:@"cell"];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text=[exercises objectAtIndex:indexPath.row];
cell.detailTextLabel.text=@"Click me...";
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 26;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
if (section == 0) return @"Letter A";
if (section == 1) return @"Letter B";
if (section == 2) return @"Letter C";
if (section == 3) return @"Letter D";
if (section == 4) return @"Letter E";
if (section == 5) return @"Letter F";
if (section == 6) return @"Letter G";
if (section == 7) return @"Letter H";
if (section == 8) return @"Letter I";
if (section == 9) return @"Letter J";
if (section == 10) return @"Letter K";
if (section == 11) return @"Letter L";
if (section == 12) return @"Letter M";
if (section == 13) return @"Letter N";
if (section == 14) return @"Letter O";
if (section == 15) return @"Letter P";
if (section == 16) return @"Letter Q";
if (section == 17) return @"Letter R";
if (section == 18) return @"Letter S";
if (section == 19) return @"Letter T";
if (section == 20) return @"Letter U";
if (section == 21) return @"Letter V";
if (section == 22) return @"Letter W";
if (section == 23) return @"Letter X";
if (section == 24) return @"Letter Y";
if (section == 25) return @"Letter Z";
return @"Other";
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
You're ignoring the section number in cellForRowAtIndexPath, so if when the tableView asks for section 1 row 3 (the fourth entry in the B section), then you're returning the fourth entry in your exercises array in the "A" section. You'll either need to restructure "exercises" to allow you to index by section (first letter), then by row, OR calculate the appropriate entry in exercises.
You should read Apple's documentation on TableViews. See "Creating and Configuring a Table View" section called "Populating an Indexed List" for help in creating an array of arrays. There's an example in Apple's TableView suite.
If you really don't want to restructure your plist into an array of arrays as suggested, then you can brute-force calculate the appropriate row in your exercises matrix as follows...
Instead of
Try: