代码工作正常但是当我为特定字母添加多个值时,它会重复出现。如果该字母表有单个值,则数据显示正确 - 我哪里出错了?
- (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;
}
答案 0 :(得分:0)
是的,你正在使用containsObject,并且总是会返回false,因为该对象的id是一个新的NSString,所以你的代码在Array中添加了相同的NSString值。尝试这样的事情:
NSArray *duplicates = [Frstname filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%C", alphabet]];
if ([duplicates count] == 0) {
[Frstname addObject:uniChar]
}
答案 1 :(得分:0)
你调试你的代码?? 由于这些行
,你得到重复的值 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];
}
这里你正在另一个for循环运行一个for循环。所以每次迭代都会调用它。 而不是for循环使用
char alphabet = [[[row valueForKey:@"FirstName"] characterAtIndex:0];
NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
if (![Frstname containsObject:uniChar]){
[Frstname addObject:uniChar];
我希望你正在编写代码以获得独特的第一个字符......
答案 2 :(得分:0)
假设您尝试从Frstname数组中的firstname插入所有非重复字符,请更改:
//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];
}
}
}
为此:
//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];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] '%c'", alphabet];
NSArray *duplicates = [Frstname filteredArrayUsingPredicate:predicate];
if ([duplicates count] == 0) {
[Frstname addObject:uniChar];
}
}
}