Objective c 按字母顺序自动组合UITableView

Objective c 按字母顺序自动组合UITableView,objective-c,uitableview,indexing,alphabetized,Objective C,Uitableview,Indexing,Alphabetized,是否有办法按字母顺序自动分组/自动分区myUITableView?我有一个巨大的数组,可以很好地显示出来,但是如果我在Contacts.app中有这样的部分会更好 最佳 –f首先定义部分 self.sections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r",

是否有办法按字母顺序自动分组/自动分区my
UITableView
?我有一个巨大的数组,可以很好地显示出来,但是如果我在Contacts.app中有这样的部分会更好

最佳

–f

首先定义部分

self.sections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];
然后

然后按字母表过滤

NSArray *sectionArray = [vendorList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.sections objectAtIndex:section]]]; 
            rowCount = [sectionArray count];
我做了以下工作:

AutoSectionTableViewDataSource.h

@protocol AutoSectionTableViewDataSource
- (NSString*)tableView:(UITableView*)tableView sectionNameAtIndexPath:(NSIndexPath*)indexPath;
@end
@interface AutoSectionTableViewDataSource : NSObject <UITableViewDataSource>
- (id)initWithDataSource:(NSObject<UITableViewDataSource,AutoSectionTableViewDataSource>*)dataSource;
@end

我有一个吊舱就是为了这个:

pod'CGLAlphabetizer',“~>0.1'

它从单个对象数组和任意键路径创建由字母表字母设置键的数组字典

AlphazedDictionary=[CGLAlphabetizer AlphazedDictionaryFromObjects:anArray usingKeyPath:@“keyPath”]

因此,假设您的模型对象如下所示:

@interface CGLContact : NSObject
@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;

@property (nonatomic, readonly) NSString *fullName;

@end
static NSString * const CGLContactsCellIdentifier = @"CGLContactsCellIdentifier";

@interface CGLContactsTableViewController ()
@property (nonatomic) NSDictionary *alphabetizedDictionary;
@property (nonatomic) NSArray *sectionIndexTitles;
@end

@implementation CGLContactsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CGLContactsCellIdentifier];
}

- (void)setContacts:(NSArray *)contacts {
    _contacts = contacts;
    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:_contacts usingKeyPath:@"lastName"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.tableView reloadData];
}

- (CGLContact *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

#pragma mark - Table view data source

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CGLContactsCellIdentifier forIndexPath:indexPath];

    CGLContact *contact = [self objectAtIndexPath:indexPath];
    cell.textLabel.text = contact.fullName;

    return cell;
}

@end
tableViewController实现可能如下所示:

@interface CGLContact : NSObject
@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;

@property (nonatomic, readonly) NSString *fullName;

@end
static NSString * const CGLContactsCellIdentifier = @"CGLContactsCellIdentifier";

@interface CGLContactsTableViewController ()
@property (nonatomic) NSDictionary *alphabetizedDictionary;
@property (nonatomic) NSArray *sectionIndexTitles;
@end

@implementation CGLContactsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CGLContactsCellIdentifier];
}

- (void)setContacts:(NSArray *)contacts {
    _contacts = contacts;
    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:_contacts usingKeyPath:@"lastName"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.tableView reloadData];
}

- (CGLContact *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

#pragma mark - Table view data source

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CGLContactsCellIdentifier forIndexPath:indexPath];

    CGLContact *contact = [self objectAtIndexPath:indexPath];
    cell.textLabel.text = contact.fullName;

    return cell;
}

@end
居住着每一个到过太空的人:


我没有找到启用自动分组的方法。所以我自己实现了它,遍历数组,并根据条目的首字母构建新的数组。你能发布你的解决方案吗?这将更清楚地表明这个问题已经解决。此外,如果它获得三张赞成票,你将因你的麻烦而获得一枚自学徽章。哦,太酷了。是的,我明天就寄。谢谢你的回复。因为我已经有一段时间没有这样做了,我不太确定,但这似乎很熟悉。我想我也采取了类似的方法。如果您需要获取某个特定部分的项目,只需要一些额外的信息,
BEGINSWITH[c]
中的
[c]
指定比较不区分大小写。您还可以使用
[cd]
,这也会使其不区分重音。搜索带有重音符号的数组时很有用。我发现这段代码的问题是,以整数开头的项目不会放在#部分。是的,如果contact以任何数字开头,这段代码将不起作用。。。有什么建议吗?
static NSString * const CGLContactsCellIdentifier = @"CGLContactsCellIdentifier";

@interface CGLContactsTableViewController ()
@property (nonatomic) NSDictionary *alphabetizedDictionary;
@property (nonatomic) NSArray *sectionIndexTitles;
@end

@implementation CGLContactsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CGLContactsCellIdentifier];
}

- (void)setContacts:(NSArray *)contacts {
    _contacts = contacts;
    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:_contacts usingKeyPath:@"lastName"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.tableView reloadData];
}

- (CGLContact *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

#pragma mark - Table view data source

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CGLContactsCellIdentifier forIndexPath:indexPath];

    CGLContact *contact = [self objectAtIndexPath:indexPath];
    cell.textLabel.text = contact.fullName;

    return cell;
}

@end