Code Monkey home page Code Monkey logo

ambtableviewcontroller's Introduction

AMBTableViewController

Storyboard and Prototype Cells-centric block-based UITableView controller to manage complex layouts.

Platform: iOS Version: 1.0.0 License: Apache 2.0 Dependency Status Build Status

Developed as part of Pecolly iOS.

Demo

A demo project is included in the repository.

Features

  • Use Storyboards' Prototype Cells to design your cells.
  • Separate table code with AMBTableViewSection's.
  • Use blocks instead of delegate calls and avoid having section code separated through multiple methods.
  • Individual hide/shown, add/remove sections and rows.
  • Support for dynamic height cells.
  • Support for special "No Content Cell"'s for empty sections.

Screenshot 1ใ€€Screenshot 2

Installation

Add the following to your CocoaPods' Podfile:

platform :ios, '5.0'

pod 'AMBTableViewController'

Documentation

http://cocoadocs.org/docsets/AMBTableViewController/

Sample Code

Part of the included demo project.

Creating and configuring sections

A section with a single "static" cell of custom height:

footerSection = [AMBTableViewSection
                 sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"footer"]]
                 sectionUpdateBlock:NULL
                 cellHeightBlock:^CGFloat(id object, NSIndexPath * indexPath) { return 120.0; }
                 cellIdentifierBlock:NULL
                 cellConfigurationBlock:NULL];

A section with a single "static" cell hidden when post is nil:

writeSection = [AMBTableViewSection
                sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"write_comment"]]
                sectionUpdateBlock:^(AMBTableViewSection * section)
                {
                    section.hidden = (weakSelf.post == nil);
                }
                cellHeightBlock:NULL
                cellIdentifierBlock:NULL
                cellConfigurationBlock:NULL];

A section with a single row of one of two kinds:

authorSection = [AMBTableViewSection
                 sectionWithObjects:@[@"author_cell"]
                 sectionUpdateBlock:^(AMBTableViewSection * section)
                 {
                     [section reloadObjectAtIndex:0];
                 }
                 cellHeightBlock:NULL
                 cellIdentifierBlock:^NSString *(id object, NSIndexPath *indexPath)
                 {
                     BOOL ownPost = [weakSelf.post.authorName isEqualToString:@"Me"];
                     return ownPost ? @"author_self" : @"author_other";
                 }
                 cellConfigurationBlock:^(id object,
                                          UITableViewCell * cell,
                                          NSIndexPath * indexPath)
                 {
                     PEPhotosDetailAuthorCell * authorCell = (PEPhotosDetailAuthorCell *)cell;
                     authorCell.authorLabel.text = weakSelf.post.authorName;
                 }],

A section with hideable cells:

NSArray * sectionObjects = @[[AMBCellIdentifier identifierFromString:@"title"],   // 0
                             [AMBCellIdentifier identifierFromString:@"image"],   // 1
                             [AMBCellIdentifier identifierFromString:@"tags"],    // 2
                             [AMBCellIdentifier identifierFromString:@"recipe"]]; // 3
topSection = [AMBTableViewSection
              sectionWithObjects:sectionObjects
              sectionUpdateBlock:^(AMBTableViewSection *section)
              {
                  [section reloadObjectAtIndex:0];
              }
              cellHeightBlock:^CGFloat(id object,
                                       NSIndexPath * indexPath)
              {
                  switch ([sectionObjects indexOfObject:object]) // Shouldn't use indexPath.row because we hide/show rows
                  {
                      case 0:
                          return 40.0;
                      case 1:
                          return 160.0;
                      case 3:
                          return 170.0;
                      default:
                          return -1.0; // Table view's default height
                  }
              }
              cellIdentifierBlock:NULL
              cellConfigurationBlock:^(id object,
                                       UITableViewCell * cell,
                                       NSIndexPath * indexPath)
              {
                  switch ([sectionObjects indexOfObject:object]) // Shouldn't use indexPath.row because we hide/show rows
                  {
                      case 0:
                      {
                          PEPhotosDetailTitleCell * titleCell = (PEPhotosDetailTitleCell *)cell;
                          titleCell.titleLabel.text = weakSelf.post.title;
                          break;
                      }
                      case 1:
                      {
                          //PEPhotosDetailImageCell * imageCell = (PEPhotosDetailImageCell *)cell;
                          break;
                      }
                  }
              }];

// Initial state
[topSection setObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]
                         hidden:YES];

A section with a dynamic number of cells of dynamic height and a special "no content cell":

commentsSection = [AMBTableViewSection
                   sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"loading_comments"]]
                   sectionUpdateBlock:NULL
                   cellHeightBlock:^CGFloat(id object,
                                            NSIndexPath * indexPath)
                   {
                       if ([object isKindOfClass:[AMBCellIdentifier class]])
                       {
                           return -1.0; // Loading comments (default height)
                       }
                       if (!object)
                       {
                           return 88.0; // No content cell
                       }
                       
                       // Dynamic height comments
                       return [weakSelf heightForCellWithIdentifier:@"comment"
                                                               text:object
                                             limitedToNumberOfLines:0];
                   }
                   cellIdentifierBlock:^NSString *(id object,
                                                   NSIndexPath * indexPath)
                   {
                       return (object ? @"comment" : // A comment
                               @"no_comments");      // No content cell
                   }
                   cellConfigurationBlock:^(id object,
                                            UITableViewCell * cell,
                                            NSIndexPath * indexPath)
                   {
                       if ([cell isKindOfClass:[PEPhotosDetailCommentCell class]] &&
                           [object isKindOfClass:[NSString class]])
                       {
                           ((PEPhotosDetailCommentCell *)cell).bodyLabel.text = (NSString *)object;
                       }
                   }];

// Enable "no content cell"
commentsSection.presentsNoContentCell = YES;

Configuring the initial table configuration

tableViewController.sections = @[topSection,
                                 authorSection,
                                 writeSection,
                                 commentsSection,
                                 footerSection];

Updating the table

Updating all sections:

- (void)setPost:(PEPost *)post
{
    _post = post;
    
    [self updateAllSections];
}

Toggling rows:

- (IBAction)toggleDetails:(id)sender
{
    // Hide if all hiddeable rows are hidden, show all otherwise
    [topSection setObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]
                             hidden:(![topSection isObjectAtIndexHidden:1] &&
                                     ![topSection isObjectAtIndexHidden:2] &&
                                     ![topSection isObjectAtIndexHidden:3])];
}

License

Copyright 2014 CyberAgent Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

ambtableviewcontroller's People

Contributors

cjwirth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.