我正在尝试在我的应用程序中复制联系人表视图。因此,我在表格视图中显示了一个联系人列表,但是我希望将表格视图分成字母表中的所有字母,并将联系人姓名放置在与其名字的列表字母相关的部分中. 像这样
我如何获得该视图?到目前为止,这就是我所做的一切。
- (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;
}
Run Code Online (Sandbox Code Playgroud)
这是使用第 3 方TLIndexPathTools数据模型类的简单解决方案TLIndexPathDataModel
。它专为处理索引路径和部分而设计,因此您可以以最小的复杂性完成所需的任务。这是一个完整的、有效的演示。
首先定义一个类来表示联系人。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
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
关键是TLIndexPathDataModel
使用@“sectionName”设置自动将数据组织成部分sectionNameKeyPath
。然后,在视图控制器逻辑中,您可以通过调用以下命令轻松访问给定索引路径的联系人:
Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
更新
您实际上想要对显示名称进行二级排序:
[contacts sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"sectionName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES]]];
Run Code Online (Sandbox Code Playgroud)
更新#2
TLIndexPathDataModel
如果您不想仅仅为了添加属性而定义自定义数据对象,那么有一个新的基于块的初始值设定项可以使这变得更加容易sectionNameKeyPath
。例如,可以使用新的初始值设定项来组织字符串列表,如“Blocks”示例项目中所示:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3685 次 |
最近记录: |