有没有办法按字母顺序自动分组/自动分段我的UITableView
?我有一个很好的数组显示,但如果我有像Contacts.app这样的部分,它会更好。按字母顺序自动分组UITableView
最佳
-f
有没有办法按字母顺序自动分组/自动分段我的UITableView
?我有一个很好的数组显示,但如果我有像Contacts.app这样的部分,它会更好。按字母顺序自动分组UITableView
最佳
-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];
然后
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 27;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.sections;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.sections objectAtIndex:section];
}
由字母是过滤后
NSArray *sectionArray = [vendorList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.sections objectAtIndex:section]]];
rowCount = [sectionArray count];
感谢您的回复。由于我已经完成了一段时间,所以我不太确定,但这似乎很熟悉。我想我采取了类似的方法。 – flohei 2011-04-10 08:05:45
我在哪里放置NSArray * sectionArray代码? – 2013-06-12 17:24:09
哪里有需要获得特定部分的项目 – yasirmturk 2013-06-13 09:23:52
我做了以下内容:
AutoSectionTableViewDataSource.h
@protocol AutoSectionTableViewDataSource
- (NSString*)tableView:(UITableView*)tableView sectionNameAtIndexPath:(NSIndexPath*)indexPath;
@end
@interface AutoSectionTableViewDataSource : NSObject <UITableViewDataSource>
- (id)initWithDataSource:(NSObject<UITableViewDataSource,AutoSectionTableViewDataSource>*)dataSource;
@end
AutoSectionTableViewDataSource.m
@interface AutoSectionTableViewDataSource()
@property(weak) NSObject<UITableViewDataSource,AutoSectionTableViewDataSource> *dataSource;
@end
@implementation AutoSectionTableViewDataSource
- (id)initWithDataSource:(NSObject<UITableViewDataSource,AutoSectionTableViewDataSource>*)dataSource
{
self = [super init];
self.dataSource = dataSource;
return self;
}
- (BOOL)respondsToSelector:(SEL)selector
{
if ([super respondsToSelector:selector]) return YES;
if (self.dataSource && [self.dataSource respondsToSelector:selector]) return YES;
return NO;
}
- (void)forwardInvocation:(NSInvocation*)invocation
{
if (self.dataSource && [self.dataSource respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:self.dataSource];
} else {
[self doesNotRecognizeSelector:invocation.selector];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
if (self.dataSource) return [self.dataSource methodSignatureForSelector:selector];
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0];
NSMutableSet *set = [[NSMutableSet alloc] init];
for (NSInteger row=0; row<rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath];
[set addObject:sectionName];
}
return set.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0];
NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init];
NSInteger count = 0;
for (NSInteger row=0; row<rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath];
if (!tmp[sectionName]) {
tmp[sectionName] = @(tmp.count);
}
if ([tmp[sectionName] intValue] == section) {
count++;
}
}
return count;
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0];
NSMutableSet *set = [[NSMutableSet alloc] init];
for (NSInteger row=0; row<rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath];
if (![set containsObject:sectionName]) {
[set addObject:sectionName];
if (set.count - 1 == section) {
//NSLog(@"AutoSectionTableViewDataSource titleForHeaderInSection:%d -> %@", section, sectionName);
return sectionName;
}
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0];
NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init];
NSInteger count = 0;
for (NSInteger row=0; row<rows; row++) {
NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:rowIndexPath];
if (!tmp[sectionName]) {
tmp[sectionName] = @(tmp.count);
}
if ([tmp[sectionName] intValue] == indexPath.section) {
count++;
if (count-1 == indexPath.row) {
UITableViewCell *cell = [self.dataSource tableView:tableView cellForRowAtIndexPath:rowIndexPath];
return cell;
}
}
}
return nil;
}
@end
,然后在你的代码:
- (NSString*)tableView:(UITableView *)tableView sectionNameAtIndexPath:(NSIndexPath *)indexPath
{
return @"this would be your section name for indexPath.row"
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return total_number_of_rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cell_for_indexPath.row;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// instead of self.tableView.dataSource = self; use:
self.autoSectionDataSource = [[AutoSectionTableViewDataSource alloc] initWithDataSource:self];
self.tableView.dataSource = self.autoSectionDataSource;
}
我有一个吊舱只为这:
https://github.com/chrisladd/CGLAlphabetizer/
pod 'CGLAlphabetizer', '~> 0.1'
它创建从对象的单个阵列和任意的keyPath由字母表的字母键阵列的字典。
alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:anArray usingKeyPath:@"keyPath"];
因此,假设你有一个看起来像这样的模型对象:
@interface CGLContact : NSObject
@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;
@property (nonatomic, readonly) NSString *fullName;
@end
你tableViewController实现可能看起来像这样:每人口居住
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
谁已去空间:
我没有找到启用自动分组的方法。所以我自己通过数组实现它,并根据它的条目的第一个字母构建新的数组。 – flohei 2010-08-03 13:22:58
你可以发布你的解决方案吗?这会让问题更清楚地解决。此外,如果它得到三个upvotes,你会得到一个自学者徽章,以解决您的麻烦。 – 2010-12-30 17:00:24
哦,很酷。是的,我明天会发布。 – flohei 2011-01-04 18:53:51