Giter Club home page Giter Club logo

pbitableview's Introduction

Artboard.png

language  Carthage compatible  Build Status  License MIT  Support

PBITableView is a kind of delightful tableview using MVVM.Compare the old ways, it provides a simply function to create the every single section, which makes creating complex tableview likes building blocks.What's more, it is convenient to provides some cell styles for static view.Hope it could help the iOS developers.

Common View we need build

WechatIMG2.jpeg

This type of tableview has several feature:

  • regular cell style but includes different user interaction component like switch , textfield ,label and so on.
  • there are many similar views in different apps.
  • writing code for this view in a waste of time and energy.

Therefor we need to improve it.

The old ways to create

self.tableview.delegate   = self;
self.tableview.datasource = self;
//Delagte and DataSource func
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cell;
}

Obviously, in this way, we need realize so many these functions and it is annoying especially we just want to realize some simple tableviews.

The new ways to create

PBITableView uses this simple functions to create:

-(void)init{
    [self initTableView];
    [self addBaseSection];
}
-(void)addBaseSection{
    [self.tableView headerView:^(PBITableViewSectionModel *sectionModel){
        //Set HeaderView
    } withCells:^(NSMutableArray *cellArray) {
        //Set CellModel Arrays
        PBITableViewCellModel *cellModel = [PBITableViewCellModel new];
        cellModel.height = 50;
        cellModel.renderBlock=^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
            //Init Cell Instance
            return cell;
            };
        }; 
        [cellArray addObject:cellModel];
    };
    } footerView:^(PBITableViewSectionModel *sectionModel){
        //Set footerView
    }];
}

Above all, it is the code about how to create a tableview with a section.All u need is realize the implementation of your cells and add the cellArray.U can set the appearance of section headerView and footerView By setting sectionModel properties.

I have written a demo in project.U can look up in detail.

The way to use manage style cells

PBIManageCell provides 5 styles cell.Each cells have their own styles too.Pick the style when init the cell. For example, I would use the PBIMangeSwitchCell to express it.

    
    //Init Func pick style through PBIMangeSwitchCellStyle
    +(PBIMangeSwitchCell *)cellWithIdentifier:(NSString *)cellIdentifier tableView:(UITableView *)tableView style:(PBIMangeSwitchCellStyle)style;
    
    //Cell Instance Implementation 
    PBIMangeSwitchCell *cell = [PBIMangeSwitchCell cellWithIdentifier:@"123" tableView:tableView style:PBIMangeSwitchCellStyleWithLeftIcon];
            cell.titleLabel.text = @"Switch Change Cell Background";
            cell.leftIcon.image = [UIImage imageNamed:@"warning"];
            __weak __typeof__(PBIMangeSwitchCell *) weak_cell = cell;
            cell.switchBlock = ^(BOOL switchValue) {
                if (switchValue == YES) {
                    weak_cell.backgroundColor = [UIColor greenColor];
                }else{
                    weak_cell.backgroundColor = [UIColor yellowColor];
                }
            };

According to the different cells, it provides aimed properties and blocks to edit.

Warning Retain cycle

Because of the block usage, please notice the retain cycle problem. u can use this __weak __typeof__(object) weak_object = object and __strong __typeof__(object) strong_object = object to avoid this problem

Code Structure

There are two main parts : PBIManageCell and PBITableView

PBIManageCell contains:

  • PBIManageCellConst.h
  • UIView+PBIManageLayout
  • PBIManageBaseCell.h
  • PBIMangeSwitchCell.h
  • PBIManageCustomCell.h
  • PBIManageLabelCell.h
  • PBIManageTextFieldCell.h

PBITableView contains:

  • PBITableView.h
  • PBITableViewModel.h
  • PBITableViewSectionModel.h
  • PBITableViewCellModel.h

Installation

#import PBIManageCell.h
#import PBITableView.h
//Then use the function in "The new ways to create" :)

License

PBITableView is released under the MIT license. See LICENSE for details.

pbitableview's People

Contributors

hyperchen avatar

Watchers

Carabineiro 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.