Hi all,
I have an app with a regular TableView. When the user selects a cell, another view is brought up, that has the content of the cell as the navigationItem.Title
That worked fine.
When I tried to make the TableView into an indexed tableView the variable failed to carry through.
Here is the indexedTableView code:
Code:
- (void)viewDidLoad {
self.navigationItem.title = @"Glossary";
IndexItems = [[NSMutableArray alloc] init];
[IndexItems addObject:@"Alpha"];
[IndexItems addObject:@"Beta"];
[IndexItems addObject:@"Charlie"];
[IndexItems addObject:@"Delta"];
[IndexItems addObject:@"Echo"];
wordIndex = [[NSMutableArray alloc] init];
for (int i=0; i<[IndexItems count]-1; i++){
//---get the first char of each word---
char alphabet = [[IndexItems objectAtIndex:i] characterAtIndex:0];
NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
//---add each letter to the index array---
if (![wordIndex containsObject:uniChar])
{
[wordIndex addObject:uniChar];
}
}
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [wordIndex objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [wordIndex objectAtIndex:section];
//---get all words beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *items = [IndexItems filteredArrayUsingPredicate:predicate];
//---return the number of words beginning with the letter---
return [items count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return wordIndex;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
NSString *alphabet = [wordIndex objectAtIndex:[indexPath section]];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *items= [IndexItems filteredArrayUsingPredicate:predicate];
if ([items count]>0) {
NSString *cellValue = [items objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedItem = [IndexItems objectAtIndex:indexPath.row];
//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
- (void)dealloc {
[IndexItems release];
[wordIndex release];
[super dealloc];
}
Thanks So much in advance!!