Dynamically switch the cell type in tableview
Why do we need to dynamically switch the cell type in tableview? If the project manager does not meet this requirement, you will not see this article :)
Effect:
Source code:
First, you need to prepare three types of cells and inherit the system directly.
//// Rootviewcontroller. M // changecell /// copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" yellowcell. H "# import" redcell. H "# import" titlecell. H "// ------------------------------ static nsstring * cell [] ={@" titlecellflag ", @" redcellflag ", @" yellowcellflag ",}; typedef Enum: nsuinteger {Title, red, yellow,} celltype; // ------------------------------ @ interface Ro Otviewcontroller () <uitableviewdatasource, uitableviewdelegate> @ property (nonatomic, strong) uitableview * tableview; @ property (nonatomic, strong) nsstring * changeflag; // switch the tag @ property (nonatomic, strong) nsarray * dataarray; // data source @ property (nonatomic, strong) nsarray * reddata; // red cell data @ property (nonatomic, strong) nsarray * yellowdata; // yellow cell data @ end @ implementation rootviewcontroller-(void) vi Ewdidload {[Super viewdidload]; // initialize tableview _ tableview = [[uitableview alloc] initwithframe: Self. view. bounds style: uitableviewstyleplain]; _ tableview. delegate = self; _ tableview. datasource = self; [self. view addsubview: _ tableview]; // red cell data _ reddata = @ [@ "", @ ""]; // yellow cell data _ yellowdata = @ [@ "", @ ", @" "]; // data source _ dataarray = _ reddata; // type _ changeflag = cell [Red]; // switch cell after 4 seconds [self defined mselector: @ selector (runselector :) withobject: Nil afterdelay: 9];}-(void) runselector :( ID) sender {// data source _ dataarray = _ yellowdata; // type _ changeflag = cell [yellow]; // reload data [_ tableview reloaddata];}-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [_ dataarray count];}-(uitableviewcell *) tableview :( uitableview *) Table View cellforrowatindexpath :( nsindexpath *) indexpath {uitableviewcell * cell = nil; If (indexpath. row = 0) // cell {Cell = [[titlecell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cell [title]; cell. textlabel. font = [uifont fontwithname: @ "helveticaneue-thin" Size: 18]; cell. textlabel. TEXT = @ "youxianming"; cell. textlabel. textcolor = [uicolor redcolor]; cell. selectionstyle = u Itableviewcellselectionstylenone;} If (indexpath. Row! = 0) // other cells {If ([_ changeflag is1_tostring: cell [Red]) {Cell = [[redcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cell [title]; cell. backgroundcolor = [uicolor redcolor]; // Red Cell. selectionstyle = regular;} If ([_ changeflag is1_tostring: cell [yellow]) {Cell = [[yellowcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cell [title]; cell. backgroundcolor = [uicolor yellowcolor]; // yellow cell. selectionstyle = uitableviewcellselectionstylenone;} return cell;}-(cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {If (indexpath. row = 0) {return 70;} If ([_ changeflag is1_tostring: cell [Red]) {return 100;} If ([_ changeflag is1_tostring: cell [yellow]) {return 200;} return 0 ;}@ end
Analysis:
Use this to mark reuse.
One label is used to switch the cell type and the corresponding data source.
Determines which cell to initialize Based on the switch tag.
This is how it is implemented.
Dynamically switch the cell type in tableview