我正在为我的 iPhone 应用程序制作联系人列表。我制作了一个包含以下属性的自定义对象类。
- 名字
- 姓氏
- 身份证
现在我想制作一个联系人列表,就像 iphone 的联系人列表一样。因此,使用 A-B-C-D-E-... 作为 TableView 部分标题。我在关注这个tutorial . 但他只是在处理弦乐。我的问题出在 CellForRow 内部。在这里你可以看到我有什么 ATM。
NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSLog(@"list content is here %@",[listContent valueForKey:@"name"]);
NSArray *contacts = [[listContent valueForKey:@"name"] filteredArrayUsingPredicate:predicate];
NSLog(@"Contacts array is %@",contacts);
Contact *contact = nil;
contact = [contacts objectAtIndex:[indexPath row]];
NSLog(@"contact is in de else %@",contact.name);
NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
我在第一行崩溃了,日志如下
2013-02-07 10:52:47.963 Offitel2[7807:907] list content is here (
Claes,
Geelen,
Verheyen
)
2013-02-07 10:52:47.964 Offitel2[7807:907] Contacts array is (
Claes
)
2013-02-07 10:52:47.964 Offitel2[7807:907] -[__NSCFString name]: unrecognized selector sent to instance 0x208e2c20
2013-02-07 10:52:47.965 Offitel2[7807:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString name]: unrecognized selector sent to instance 0x208e2c20'
谁能帮我解决这个问题?
亲切的问候
最佳答案
请按照以下步骤操作:
- 制作一个索引字母表数组,其中包含您要填充的单词的所有起始字母表
现在排序:
//sorting for english language indexAlphabetArray = (NSMutableArray *)[indexAlphabetArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
现在实现
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return indexAlphabetArray; }
最后的建议,取一个所有名字和组名的字典,以首字母作为字典的关键字,例如:
nameDictionary:{ A:( Astart, Astop, ) b:( bstart, bstop, ) }
这样
indexAlphabetArray = [nameDictionary allKeys];
编辑:让您更轻松:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[nameDictionary valueForKey:[indexAlphabetArray objectAtIndex:section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
}
NSArray *nameArray = [nameDictionary valueForKey:[indexAlphabetArray objectAtIndex:indexPath.section]];
cell.textLabel.text = [nameArray objectAtIndex:indexPath.row];
return cell;
}
关于iphone - 使用对象按字母顺序对 tableview 进行排序并显示按字母顺序排列的 tableview 标题,如通讯录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748173/