GHUITable 0.1.13

GHUITable 0.1.13

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2016

Maintained by Gabriel Handford.



GHUITable 0.1.13

  • By
  • Gabriel Handford

Making UITableView, UICollectionView, UITableViewCell easier to use.

Also includes a UITableView for use with YapDatabase.

Podfile

pod "GHUITable"

pod "GHUITable/Yap" # Optional, for YapDatabase support

Example Project

The best way to follow and learn about GHUITable is by seeing it in action. Clone the repo and open the Example project.

Usage

GHUITableView

GHUITableView *tableView = [[GHUITableView alloc] init];

// These are all the cells that will be used, usually it's just one.
[tableView registerClasses:@[GHUITextImageCell.class]];

// This block returns the cell class.
// It can be based on the data or section, or (in this case) can be constant.
tableView.dataSource.classBlock = ^Class(id object, NSIndexPath *indexPath) {
  return GHUITextImageCell.class;
};

// Set the data on the cell.
tableView.dataSource.cellSetBlock = ^(GHUITextImageCell *cell, GHData *data, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
  [cell.viewForContent setName:data.name description:data.description image:[UIImage imageNamed:data.imageName]];
};

// Set what happens when a user selects a cell.
tableView.dataSource.selectBlock = ^(UITableView *tableView, NSIndexPath *indexPath, GHData *data) {
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  NSLog(@"Selected: %@", indexPath);
  // Do something
};

// Set section headers
[tableView.dataSource setHeaderText:@"Section 1" section:0];
[tableView.dataSource setHeaderText:@"Section 2" section:1];

Adding data

There are many methods for adding, updating or removing data.

[tableView setObjects:@[...] animated:NO];
[tableView addObjects:@[...] animated:NO];
// And many more...

Or access methods on the datasource. (You can pass nil for indexPaths if you don't care what changed.)

[tableView.dataSource addObjects:@[..] section:0 indexPaths:nil];
[tableView.dataSource removeObjects:@[..] section:0 indexPaths:nil];
// And many more...

// If you edit the datasource directly be sure to call reloadData
[tableView reloadData];

GUITableViewCell

This allows you to turn any view into a UITableViewCell in just a few lines of code.

For example, GHUITextImageCell is defined as:

@interface GHUITextImageCell : GHUITableViewCell
@end

@implementation GHUITextImageCell
+ (Class)contentViewClass { return GHUITextImageView.class; }
@end

GHUITextImageView is also an example of a view using YOLayout which you should also check out!

GHUITableView (More complex example)

He is an example with different cell classes for different sections.

GHUITableView *tableView = [[GHUITableView alloc] init];

// These are the cells that will be used.
[tableView registerClasses:@[GHUISwitchCell.class, GHUITextImageCell.class]];

// This block chooses the cell class based on the section, but you could also do it based on the data.
tableView.dataSource.classBlock = ^Class(NSDictionary *object, NSIndexPath *indexPath) {
  if (indexPath.section == 2) return GHUISwitchCell.class;
  return GHUITextImageCell.class;
};

// Set the data on the cell. (Cell depends on section.)
tableView.dataSource.cellSetBlock = ^(GHUITableViewCell *cell, NSDictionary *dict, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
  if (indexPath.section == 0) {
    [cell.viewForContent setName:dict[@"name"] description:dict[@"description"] image:[UIImage imageNamed:dict[@"imageName"]]];
  } else if (indexPath.section == 1) {
    [cell.viewForContent setName:dict[@"name"] description:dict[@"description"] imageURLString:dict[@"imageURLString"]];
  } else if (indexPath.section == 2) { // or [cell isKindOfClass:GHUISwitchCell.class]
    [cell.viewForContent setTitle:dict[@"title"] description:dict[@"description"] on:[dict[@"on"] boolValue]];
  }
};

GHUITableView (Static Content)

Sometimes it's useful to use table views even when you have static content. You can add a UIView to the data source instead of data. This bypasses the cell rendering pattern so it's not appropriate for when you have tons of content.

GHUITableView *tableView = [[GHUITableView alloc] init];

// You can add a view as a data object source.
// In this scenario you are not re-using views and is appropriate for when you have static content.
GHUITextImageView *view = [[GHUITextImageView alloc] init];
[view setName:@"Name2" description:@"This is a description #2" image:[UIImage imageNamed:@"Preview2-Filled"]];
[tableView addObjects:@[view] section:0 animated:NO];

// You can even add a cell as a data object source (although this is probably a little awkward, can be useful sometimes).
GHUITextImageCell *cell = [[GHUITextImageCell alloc] init];
[cell.viewForContent setName:@"Name1" description:@"This is a description #1" image:[UIImage imageNamed:@"Preview2"]];
[tableView addObjects:@[cell] section:1 animated:NO];

GHUICollectionView

GHUICollectionView *view = [[GHUICollectionView alloc] init];
view.backgroundColor = [UIColor grayColor];

// These are all the cells that will be used, usually it's just one.
[view registerClasses:@[GHUITextImageCollectionCell.class]];

// This block returns the cell class.
// It can be based on the data or section, or (in this case) can be constant.
view.dataSource.classBlock = ^Class(id object, NSIndexPath *indexPath) {
  return GHUITextImageCollectionCell.class;
};

// Set the data on the cell.
view.dataSource.cellSetBlock = ^(GHUITextImageCollectionCell *cell, NSDictionary *dict, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
  [cell.viewForContent setName:dict[@"name"] description:dict[@"description"] image:[UIImage imageNamed:dict[@"imageName"]]];
};

// Set what happens when a user selects a cell.
view.dataSource.selectBlock = ^(UICollectionView *collectionView, NSIndexPath *indexPath, NSString *object) {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
  NSLog(@"Selected: %@", indexPath);
  // Do something
};

// Set header view
[_view registerHeaderClass:GHUICollectionHeaderLabelView.class];
  _view.dataSource.headerViewBlock = ^(UICollectionView *collectionView, GHUICollectionHeaderLabelView *view, NSInteger section) {
  view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
  view.label.textColor = [UIColor colorWithRed:255.0f/255.0f green:125.0f/255.0f blue:0.0f/255.0f alpha:1.0];
  view.label.font = [UIFont systemFontOfSize:18];
  view.label.text = [NSString stringWithFormat:@"Section %@", @(section)];
};

// The data
[_view setObjects:
  @[
    @{@"name": @"Gastropub swag pork belly, butcher selvage mustache chambray scenester pour-over.",
    @"description": @"Cosby sweater stumptown Carles letterpress, roof party deep v gastropub next level. Tattooed bitters distillery, scenester PBR&B pork belly swag twee DIY. Mixtape plaid Carles photo booth sustainable you probably haven't heard of them. Vice normcore fap Thundercats Williamsburg Truffaut paleo small batch, plaid PBR&B Brooklyn jean shorts. Next level lomo direct trade farm-to-table, cred hoodie post-ironic fingerstache pop-up put a bird on it. Keytar PBR literally, DIY Bushwick Pinterest bicycle rights.",
    @"imageName": @"Preview2"},
    @{@"name": @"YOLO irony beard",
    @"description": @"Raw denim Tumblr roof party beard gentrify pickled, art party ethical",
    @"imageName": @"Preview2"}
  ] section:0];

GHUIYapTableView

Example comming soon.