Autogroup UITableView alphabetically

前端 未结 3 593
無奈伤痛
無奈伤痛 2020-12-13 11:32

Is there a way to auto group/auto-section my UITableView alphabetically? I have a huge array which is being displayed nicely, but it would be even better if I h

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 11:53

    I have a pod just for this:

    https://github.com/chrisladd/CGLAlphabetizer/

    pod 'CGLAlphabetizer', '~> 0.1'

    It creates a dictionary of arrays keyed by letters of the alphabet from a single array of objects and an arbitrary keyPath.

    alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:anArray usingKeyPath:@"keyPath"];

    So, assuming you had the model object that looked like this:

    @interface CGLContact : NSObject
    @property (nonatomic) NSString *firstName;
    @property (nonatomic) NSString *lastName;
    
    @property (nonatomic, readonly) NSString *fullName;
    
    @end
    

    Your tableViewController implementation might look something like this:

    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
    

    Populated with every human who has gone to space:

    demo

提交回复
热议问题