5

我正在尝试在我的应用程序中复制联系人表视图。因此,我在表格视图中显示了一个联系人列表,但是我希望将表格视图分成字母表中的所有字母,并将联系人的姓名放在与其名字的列表字母相关的部分中. 像这样

在此处输入图像描述

我如何获得这种观点?到目前为止,这就是我所做的一切。

- (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;
}
4

1 回答 1

3

这是使用第 3 方TLIndexPathTools数据模型类的简单解决方案TLIndexPathDataModel。它专为处理索引路径和部分而设计,因此您可以以最小的复杂性完成所需的工作。这是一个完整的工作演示

首先定义一个类来表示一个联系人。这为您提供了一个定义firstNamelastNamedisplayName的地方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

关键是使用设置为@“sectionName”TLIndexPathDataModel自动将您的数据组织成部分。sectionNameKeyPath然后在您的视图控制器逻辑中,您可以通过调用轻松访问给定索引路径的联系人:

Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];

更新

您实际上想对显示名称进行二级排序:

[contacts sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"sectionName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES]]];

更新#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];
}
于 2013-09-02T19:21:27.823 回答