Giter Site home page Giter Site logo

yltableview's Introduction

Build Status

YLTableView

YLTableView is a framework for working with UITableView used by a number of the table views inside of the Yelp app. It makes it easy to work with cell models, allows your cells to have child view controllers, and provides a framework for building your own pull-to-refresh header.

To learn more about the goals high-level architecture of YLTableView, check out our blog post.

Installation

We recommend using CocoaPods to install YLTableView. To do so, add the following to your Podfile:

pod 'YLTableView', '~> 2.2.0'

Example usage

You can clone the project and check out the example app to see YLTableView in use.

Cells and Models

All cells should provide an estimated height and take a model. Cells generally expect one specific type of model, while a given model could work with any number of cells. Start by subclassing UITableViewCell and implementing the YLTableViewCell protocol, which requires implementing setModel: and estimatedRowHeight::

@interface YLExampleTextCell : UITableViewCell <YLTableViewCell>
@end

@implementation YLExampleTextCell
- (void)setModel:(NSObject *)model {
  NSAssert([model isKindOfClass:[NSString class]], @"Must use %@ with %@", NSStringFromClass([NSString class]), NSStringFromClass([self class]));
  self.mainTextLabel.text = (NSString *)model;
}

+ (CGFloat)estimatedRowHeight {
  return 44.0;
}
@end

This cell expects an NSString as its model, but your cell can use any object you want.

Data Source

To use YLTableView, you should subclass YLTableViewDataSource. After implementing a few simple methods, YLTableViewDataSource takes care of connecting cells to models.

@interface YLExampleTableViewDataSource : YLTableViewDataSource
@property (copy, nonatomic) NSArray *models;
@end

@implementation YLExampleTableViewDataSource
- (instancetype)init {
  if (self = [super init]) {
    _models = @[@"cell one", @"cell two", @"cell three"];
  }
  return self;
}

#pragma mark YLTableViewDataSource subclass

- (NSString *)tableView:(UITableView *)tableView reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath {
  return NSStringFromClass([YLExampleTextCell class]);
}

- (NSObject *)tableView:(UITableView *)tableView modelForCellAtIndexPath:(NSIndexPath *)indexPath {
  // The model returned here must work with the cell specified above.
  return self.sectionModels[indexPath.row];
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.sectionModels.count;
}
@end

Table View

Finally, you can use a YLTableView to connect everything together.

@interface YLExampleViewController : UIViewController
@end

@implementation YLExampleViewController
- (void)loadView {
  [super loadView];
  YLExampleTableViewDataSource *dataSource = [[YLExampleTableViewDataSource alloc] init];
  YLTableView *tableView = [[YLTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  // YLTableViewDataSource has to be the delegate and the dataSource of the table view.
  tableView.dataSource = dataSource;
  tableView.delegate = dataSource;

  // Registering cells and reuse identifiers
  [tableView registerClass:[YLExampleTextCell class] forCellReuseIdentifier:NSStringFromClass([YLExampleTextCell class])];

  self.view = tableView;
}
@end

Child View Controllers

In order to make your cells more modular, YLTableViewDataSource allows your cells to have child view controllers. Simply have your cell implement the YLTableViewChildViewControllerCell protocol and set the parentViewController property on your data source. YLTableViewDataSource will take care of the rest.

This is great when your cells have multiple buttons or actions that can push on view controllers. For example, the cell below is a UICollectionView owned by a UIViewController, which pushes on larger image views when the user taps on an image.

Images cell

Check out YLExampleImagesCell.m for an example.

Pull to Refresh

YLTableView doesn't include a pull-to-refresh header, but makes it really easy to build your own. Simply subclass YLRefreshHeaderView and implement setRefreshState:animated:. Then you can set the refreshHeader property on YLTableView and add a listener for the UIControlEventValueChanged event on your refresh header.

Check out YLExampleRefreshHeader.m for an example.

License

Copyright 2016 Yelp, 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.

yltableview's People

Contributors

benasher44 avatar bmelts avatar elliottwilliams avatar jawwad avatar mglidden avatar scfonowy avatar sirarkimedes avatar ssheldon avatar ushhud avatar ytok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yltableview's Issues

Small recommendation on what to include in actual repo

I'm not sure if this was due to an order of events I attempted to open the workspace or an out of date cocoapods version I was originally using but I kept getting the following error.

[!] Unable to satisfy the following requirements:

  • YLTableView (from../) required by Podfile
  • YLTableView (= 1.0.0) required by Podfile.lock

Wonder if it would be better to not include the workspace and lock. Or include them, but include the Pods already. Got around this issue by deleting the workspace then reinstalling the pods.

Support cell nibs

UITableView allows you to register a nib for your table view cell, but we don't support this in YLTableView.

childViewController does not support UITableViewController

It seem that if the childViewController is kind of UITableViewController then the height of cell is wrong .And Xcode will output the following log

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

Constraints of childViewController can't not get the height,because the childViewController's height is determined by the tableView which is determined by the content size .

[Question] Modifying the autolayout engine from background thread error

I have utilised the YLTableView example app as a guide to implement YLTableView in MyApp.

On implementation - I have run into the below mentioned error.

As found in these StackOverflow questions

1. This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

2. Getting a “This application is modifying the autolayout engine” error?


MyApp Log

2016-08-10 11:56:10.923 MyApp[1955:1065939] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.
 Stack:(
    0   CoreFoundation                      0x00000001827d5918 <redacted> + 148
    1   libobjc.A.dylib                     0x0000000181e43f80 objc_exception_throw + 56
    2   CoreFoundation                      0x00000001827d5848 <redacted> + 0
    3   Foundation                          0x00000001832732d4 <redacted> + 88
    4   Foundation                          0x00000001830f547c <redacted> + 36
    5   UIKit                               0x00000001875cf5e0 <redacted> + 64
    6   UIKit                               0x00000001875cf824 <redacted> + 548
    7   UIKit                               0x00000001875cf5f0 <redacted> + 80
    8   UIKit                               0x00000001874c2a38 <redacted> + 240
    9   UIKit                               0x000000018760fa98 <redacted> + 640
    10  UIKit                               0x00000001875cc37c <redacted> + 68
    11  UIKit                               0x00000001875d3078 <redacted> + 28
    12  UIKit                               0x00000001875d6108 <redacted> + 364
    13  MyApp                             0x000000010011f5c4 -[MyAppCell _installConstraints] + 384
    14  MyApp                             0x000000010011f3f4 -[MyAppCell initWithStyle:reuseIdentifier:] + 568
    15  UIKit                               0x0000000187613be0 <redacted> + 528
    16  UIKit                               0x000000018764fbd4 <redacted> + 88
    17  MyApp                             0x00000001002e2004 -[YLTableViewDataSource tableView:cellForRowAtIndexPath:] + 120
    18  UIKit                               0x000000018781931c <redacted> + 692
    19  UIKit                               0x0000000187819484 <redacted> + 80
    20  UIKit                               0x00000001878087e8 <redacted> + 2360
    21  UIKit                               0x000000018781dfb0 <redacted> + 104
    22  UIKit                               0x00000001875b308c <redacted> + 176
    23  MyApp                             0x00000001002e17fc -[YLTableView layoutSubviews] + 60
    24  UIKit                               0x00000001874c3778 <redacted> + 656
    25  QuartzCore                          0x0000000184ed2b2c <redacted> + 148
    26  QuartzCore                          0x0000000184ecd738 <redacted> + 292
    27  QuartzCore                          0x0000000184ecd5f8 <redacted> + 32
    28  QuartzCore                          0x0000000184eccc94 <redacted> + 252
    29  QuartzCore                          0x0000000184ecc9dc <redacted> + 512
    30  QuartzCore                          0x0000000184efbae0 <redacted> + 236
    31  libsystem_pthread.dylib             0x000000018243e1e0 <redacted> + 584
    32  libsystem_pthread.dylib             0x000000018243dd58 <redacted> + 136
    33  libsystem_pthread.dylib             0x000000018243d53c pthread_mutex_lock + 0
    34  libsystem_pthread.dylib             0x000000018243d020 start_wqthread + 4
)

Have you seen this issue before? If so, any ideas on where to investigate further?

Many Thanks,
Shane

Should YLTableViewCell accept nil models?

-[YLTableViewCell setModel:] is annotated as nullable, but I can't come up with a legitimate scenario where it would make sense to call setModel: with a nil model.

In fact, tableView:modelForCellAtIndexPath: is actually marked as nonnull, so that wouldn't be a source of nil.

Unregistering cell classes causes an assertion failure

The UITableView docs for registerClass:forCellReuseIdentifier: say that:

You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

However, attempting that with YLTableView causes an assertion failure:

You can only use cells conforming to YLTableViewCell.

The assertion is triggered because, well, nil isn't a YLTableViewCell.

cellClassForReuseIdentifier ivar is never used

In YLTableView.m, we override registerClass:forCellReuseIdentifier: so that we can keep our own cellClassForReuseIdentifier dictionary up to date, but then never read from that dictionary.

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.