• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plugin of UITableView can easy manage models and auto calculate cell height with autolayout

NJEasyTable

A plugin of UITableView can easy manage models and auto calculate cell height with autolayout

install

use cocoapods

pod  'NJEasyTable'

Usage

#import "UITableView+NJEasyTable.h"

Set up models

You can add sections and rows to nj_TableModel.

[self.tableView.nj_TableModel addSection:({
        NJEasyTableSection *section = [[NJEasyTableSection alloc] initWithModel:nil];
        [section addRow:({
            NJEasyTableRow *row = [[NJEasyTableRow alloc] initWithModel:model];
            row.cellHeight = 44;
            row;
        })];
        section;
})];

Get models in dataSource and delegate method of UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [tableView.nj_TableModel numberOfSections];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableView.nj_TableModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier";
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    id model = [tableView.nj_TableModel modelAtIndexPath:indexPath];
    [cell configWithModel:model];
    
    return cell;
}

Dynamic cell heights

If you use autolayout in UITableViewCell. you doesn't need to calculate cell height manually. Cell height value can like this:

// regist a cell for calculate cell height
TestCell *cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[self.tableView nj_registAutolayoutCell:cell forAutomaticCalculationHeightIdentifier:@"testCell"];

// implement in heightForRow
- (GFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = [tableView nj_intrinsicHeightAtIndexPath:indexPath
                                                forIdentifier:@"testCell"
                                              configCellBlock:^(TestCell *cell, id model) {
                                                  [cell configWithModel:model];
                                              }];
    return height;
}