Dynamically switch the cell type in tableview

Source: Internet
Author: User

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

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.