Custom cell Upgrade in iOS (Advanced)

Source: Internet
Author: User
Tags allkeys uikit


The last shared custom cell was optimized: http://blog.csdn.net/qq_31810357/article/details/49611255

To specify the root view:

    Self.window.rootViewController = [[Uinavigationcontroller alloc] initwithrootviewcontroller:[[ Roottableviewcontroller alloc] Initwithstyle:uitableviewstyleplain];

Roottableviewcontroller.m

#import "WGModel.h" #import "WGCell.h" @interface Roottableviewcontroller () @property (nonatomic, Strong) Nsmutabledictionary *datadict; @end @implementation roottableviewcontroller-(void) viewdidload{    [Super Viewdidload];        Self.datadict = [Nsmutabledictionary dictionary];        [Self.tableview Registerclass:[wgcell class] forcellreuseidentifier:@ "cell"];    [Self loaddataandshow];}

Request data:

-(void) loaddataandshow{nsurl *url = [Nsurl urlwithstring:@ "http://api.breadtrip.com/trips/2387133727/schedule/"];    Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; [Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {if (data! = nil) {Nsarray *ar            Ray = [Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil];                For (nsdictionary *dict in array) {NSString *key = dict[@ "Date"];                Nsarray *placesarray = dict[@ "Places"];                Nsmutablearray *mutablearray = [Nsmutablearray array];                    For (Nsdictionary *placesdict in Placesarray) {Wgmodel *model = [[Wgmodel alloc] init];                    [Model Setvaluesforkeyswithdictionary:placesdict];                    Model.isshow = NO; [Mutablearray AddObject: Model];            } [Self.datadict Setobject:mutablearray Forkey:key];        } [Self.tableview Reloaddata]; }                    }];}

#pragma mark-table View data source

-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{    return self.dataDict.allKeys.count;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{nsstring    *key = Self.datadict.allkeys[section];    return [Self.datadict[key] count];} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    Wgcell * cell = [TableView dequeuereusablecellwithidentifier:@ "cell" forindexpath:indexpath];        NSString *key = self.datadict.allkeys[indexpath.section];    Nsmutablearray *mutablearray = Self.datadict[key];    Wgmodel *model = Mutablearray[indexpath.row];    [Cell Configurecellwithmodel:model];        if (model.isshow = = YES) {        [cell Showtableview];    } else {                [cell Hiddentableview];    }        return cell;}

self-adapting high

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{    nsstring *key = Self.datadict.allkeys[indexpath.section];    Nsmutablearray *mutablearray = Self.datadict[key];    Wgmodel *model = Mutablearray[indexpath.row];    if (model.isshow) {        return (Model.pois.count + 1) *,    } else {        return;}    }

Click the cell will go the way

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    nsstring *key = Self.datadict.allkeys[indexpath.section];    Nsmutablearray *mutablearray = Self.datadict[key];    Wgmodel *model = Mutablearray[indexpath.row];    Model.isshow =!model.isshow;    [Self.tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];}

Custom Cell

. h#import <UIKit/UIKit.h> @class Wgmodel; @interface Wgcell:uitableviewcell<uitableviewdatasource, Uitableviewdelegate> @property (nonatomic, strong) UILabel *alabel; @property (nonatomic, strong) UITableView * tableview;-(void) Configurecellwithmodel: (Wgmodel *) model;-(void) showtableview;-(void) Hiddentableview; @end//.m# Import "WGCell.h" #import "WGModel.h" @interface Wgcell () @property (nonatomic, strong) Nsmutablearray *dataarray;@ End@implementation wgcell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{if (self = [super Initwithstyle:style Reuseidentifier:reuseidentifier]) {Self.dataarray = [NSM        Utablearray array];    [Self addallviews]; } return self;} -(void) addallviews{Self.alabel = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, [UIScreen mainscreen].bounds.size.wi    DTH, 44)];    Self.aLabel.backgroundColor = [Uicolor Greencolor];    [Self.contentview AddSubview:self.aLabel]; Self.tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, A, [UIScreen mainscreen].bounds.size.width, 0) style:        Uitableviewstyleplain];    Self.tableView.delegate = self;    Self.tableView.dataSource = self; [Self.tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "Testcell"];//[Self.contentView AddSubview:self.tableView];} -(void) showtableview{[Self.contentview AddSubview:self.tableView];} -(void) hiddentableview{[Self.tableview Removefromsuperview];}    -(void) Configurecellwithmodel: (Wgmodel *) model{[Self.dataarray removeallobjects];        Self.aLabel.text = model.place[@ "name"];    Nsarray *array = Model.pois;        For (nsdictionary *dict in array) {NSString *str = dict[@ "name"];    [Self.dataarray ADDOBJECT:STR];    } CGRect frame = Self.tableView.frame;    frame.size.height = * ARRAY.COUNT;    Self.tableView.frame = frame;    [Self.tableview Reloaddata]; }-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{RETUrn 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return Self.dataArray.count;}    -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Testcell" Forindexpath:indexpath];    NSString *str = Self.dataarray[indexpath.row];    Cell.textLabel.text = str; return cell;}

prepare a model class

. H#import <Foundation/Foundation.h> @interface wgmodel:nsobject@property (nonatomic, assign) BOOL isshow;@ Property (Nonatomic, Strong) Nsdictionary *place; @property (nonatomic, strong) Nsarray *pois; @end//.m#import "WGModel.h "@implementation wgmodel-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key{    } @end

Final effect:



daily update concerns:Http://weibo.com/hanjunqiang Sina Weibo


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Custom cell Upgrade in iOS (Advanced)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.