的代碼工作正常,但是當我對一個特定的字母添加一個以上的值,它就會被重複。如果該字母表有單個值,則數據顯示正確 - 我哪裏錯了?排序值不正確歌廳顯示的tableview在iphone
- (void)viewDidLoad {
[super viewDidLoad];
PeopleModal *dislist = [[PeopleModal alloc]init];
[dislist getAll];
personarray = [[NSMutableArray alloc]init];
[personarray addObjectsFromArray:dislist.Peoplelistarray];
//short the personarray value:
NSSortDescriptor *asortDescriptor;
asortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"FirstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors=[NSArray arrayWithObject:asortDescriptor];
[self.personarray sortUsingDescriptors:sortDescriptors];
//---create the index---
Frstname = [[NSMutableArray alloc] init];
//check
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSDictionary *row in personarray) {
[tempArray addObject:[row valueForKey:@"FirstName"]];
for (int i=0; i<[tempArray count]-1; i++){
char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0];
NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
if (![Frstname containsObject:uniChar]){
[Frstname addObject:uniChar];
}
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [Frstname count];
}
//---set the title for each section---
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [Frstname objectAtIndex:section];
}
//---set the index for the table---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return Frstname;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (isSearchOn) {
return [searchResult count];
} else{
//return personarray.count;
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [Frstname objectAtIndex:section];
//---get all FirstName beginning with the letter---beginswith
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF.FirstName beginswith[c] %@", alphabet];
NSArray *Names = [personarray filteredArrayUsingPredicate:predicate];
//---return the number of Names beginning with the letter---
return [Names count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
else {
//---get the letter in the current section---
NSString* alphabet = [Frstname objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF.FirstName beginswith[c] %@", alphabet];
//[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
//NSArray *Names = [[personarray valueForKey:@"FirstName"] filteredArrayUsingPredicate:predicate];
NSArray *Names = [personarray filteredArrayUsingPredicate:predicate];
//NSArray *lastNames = [[personarray valueForKey:@"LastName"] filteredArrayUsingPredicate:predicate];
if ([Names count]>0) {
//---extract the relevant firstname from the Names object---
PeopleModal *locallist = [Names objectAtIndex:indexPath.row];
cell.textLabel.text = locallist.FirstName;
cell.detailTextLabel.text=locallist.LastName;
}
}
return cell;
}
你可以編輯你的答案,並保留代碼塊的第一行嗎?這很難閱讀。 – Madhu 2012-03-15 09:14:17