AMBTableViewController 1.2.1

AMBTableViewController 1.2.1

测试已测试
语言语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2017年10月

Ernesto RiveraErnesto Rivera 维护。




  • CyberAgent Inc. 和 Ernesto Rivera

以 Storyboard 和 Prototype Cells 为中心的 blocks-based UITableView 控制器,用于管理复杂布局。

Dependency Status

作为 Pecolly iOS 的一部分开发。

演示

一个示例项目包含在仓库中。

功能

  • 使用 Storyboards 的原型单元格来设计单元格。
  • 通过 AMBTableViewSection 分离表代码。
  • 使用 blocks 代替 delegate 调用,避免通过多个方法分离 section 代码。
  • 单独隐藏/显示,添加/删除 sections 和 rows。
  • 支持动态高度的单元格。
  • 支持特殊 "无内容单元格" 用于空 sections。

截图 1 截图 2

安装

将以下内容添加到您的 CocoaPodsPodfile

platform :ios, '5.0'

pod 'AMBTableViewController'

文档

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

示例代码

包含在示例项目中的部分。

创建和配置 sections

具有单个自定义高度的 "静态" 单元格的 section

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

当 post 是 nil 时隐藏的单个 "静态" 单元格的 section

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

具有两种类型之一单行的 section

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;
                 }],

具有可隐藏单元格的 section

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];

一个具有动态单元格数量和动态高度的节,以及一个特殊的“无内容单元格”

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;

配置初始表格设置

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

更新表格

更新所有部分

- (void)setPost:(PEPost *)post
{
    _post = post;

    [self updateAllSections];
}

切换行

- (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])];
}

许可

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.