我正在尝试在我的应用中复制联系人表格视图。所以我有一个表格视图中显示的联系人列表,但我希望将表格视图划分为字母表中的所有字母以及要放在与其名字的列表字母相关的部分中的联系人姓名。喜欢这个
我如何获得该视图?到目前为止,这就是我所做的一切。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [displayNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ContactsCell";
/*ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// cell.name.text = [displayNames
// objectAtIndex:indexPath.row];
UILabel *namer = (UILabel *)[cell viewWithTag:101];
namer.text=[displayNames
objectAtIndex:indexPath.row];
/*
Get the picture urls from the picture array and then
I loop through the array and initialize all the urls with
a NSUrl and place the loaded urls in anouther nsmutable array
*/
urls = [[NSMutableArray alloc] init];
for (id object in pictures) {
//NSDictionary *names = res[@"image"];
NSString *name = object;
NSURL *url=[[NSURL alloc] initWithString:name];
[urls addObject:url];
}
// cell.profile.image= [UIImage imageWithData:[NSData dataWithContentsOfURL: [urls objectAtIndex:indexPath.row]]];
UIImageView *profiler = (UIImageView *)[cell viewWithTag:100];
profiler.image= [UIImage imageWithData:[NSData dataWithContentsOfURL: [urls objectAtIndex:indexPath.row]]];
return cell;
}
答案 0 :(得分:3)
以下是使用第三方TLIndexPathTools数据模型类TLIndexPathDataModel
的简单解决方案。它专门用于处理索引路径和部分,因此您可以以最小的复杂性完成所需的操作。这是一个full, working demo。
首先定义一个表示联系人的类。这为您提供了定义firstName
,lastName
,displayName
和sectionName
的位置:
@interface Contact : NSObject
@property (strong, nonatomic, readonly) NSString *firstName;
@property (strong, nonatomic, readonly) NSString *lastName;
@property (strong, nonatomic, readonly) NSString *displayName;
@property (strong, nonatomic, readonly) NSString *sectionName;
- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
@end
sectionName
属性只返回firstName
的第一个字符。然后,如果您的表视图子类TLTableViewController
,则实现将如下所示:
@implementation ContactsTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *contacts = [NSMutableArray array];
//get actual list of contacts here...
[contacts addObject:[[Contact alloc] initWithFirstName:@"John" lastName:@"Doe"]];
[contacts addObject:[[Contact alloc] initWithFirstName:@"Sally" lastName:@"Smith"]];
[contacts addObject:[[Contact alloc] initWithFirstName:@"Bob" lastName:@"Marley"]];
[contacts addObject:[[Contact alloc] initWithFirstName:@"Tim" lastName:@"Cook"]];
[contacts addObject:[[Contact alloc] initWithFirstName:@"Jony" lastName:@"Ives"]];
[contacts addObject:[[Contact alloc] initWithFirstName:@"Henry" lastName:@"Ford"]];
//sort by section name
[contacts sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"sectionName" ascending:YES]]];
//set the data model
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:contacts sectionNameKeyPath:@"sectionName" identifierKeyPath:nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
//get contact for index path from data model and configure cell
Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
cell.textLabel.text = contact.displayName;
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexPathController.dataModel.sectionNames;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
@end
关键的事情是,TLIndexPathDataModel
自动使用sectionNameKeyPath
设为@ “sectionName” 组织数据段。然后在视图控制器逻辑中,您可以通过调用:
Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
<强>更新强>
您实际上想要对显示名称进行二级排序:
[contacts sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"sectionName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES]]];
更新#2
有是一个新的基于块的初始化TLIndexPathDataModel
,使得这更容易很多,如果你不希望自定义一个数据对象只是添加sectionNameKeyPath
属性。例如,可以使用新的初始值设定项来组织字符串列表,如"Blocks" sample project中所示:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *items = [@[
@"Fredricksburg",
@"Jelly Bean",
...
@"Metadata",
@"Fundamental",
@"Cellar Door"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
//generate section names by taking the first letter of each item
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:items
sectionNameBlock:^NSString *(id item) {
return [((NSString *)item) substringToIndex:1];
} identifierBlock:nil];
}