IOS UITableView Usage Summary

Source: Internet
Author: User

Define your own TableCell way summary, related to use TableView a lot of tutorials, the following talk about my experience in the use of the process, I hope to help #import "ViewController.h" #import "    TableViewCell.h "@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate>{BOOL type; int number;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return number;} static NSString *cellidf1 = @ "Cell1", static nsstring *cellidf2 = @ "Cell2"; static BOOL nibsregistered = no;-(uitableviewcel L *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{/** Code Way created *//** Uitablev    Iewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidf1]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid        F1]; UILabel *title = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, Cgrectgetwidth (cell.frame), Cgrectgetheight (Cell.frame))];        [Title settag:200];    [Cell Addsubview:cell];    } UILabel *titlelb = (UILabel *) [cell viewwithtag:200];    [Titlelb settext:@ "test"];    *//** xib Way *//** UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidf1]; if (cell = nil) {//testcellxib is the Xib file name for UITableViewCell cell = [[[NSBundle Mainbundle] loadnibnamed:@ "tes    Tcellxib "Owner:nil Options:nil] lastobject];    } UILabel *titlelb = (UILabel *) [cell viewwithtag:300];     [Titlelb settext:@ "Xibcelltest"];    * */** UITableViewCell sub-class combined with xib way *//** static NSString *customcellidentifier = @ "Customcellidentifier";    Customcell *cell = [TableView dequeuereusablecellwithidentifier:customcellidentifier];        if (cell = = nil) {Nsarray *xib = [[NSBundle mainbundle] loadnibnamed:@ "Customcell" Owner:nil Options:nil];    cell = [Xib lastobject]; } *//** using inheritance UitablevIewcell sub-Class Customcell *//** static NSString *customcellidentifier = @ "Customcellidentifier";     Customcell *cell = [TableView dequeuereusablecellwithidentifier:customcellidentifier]; if (cell = = nil) {cell = [[Customcell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:customcelli     Dentifier]; }///////register with the registration of the use of the registration of the section can be put into the viewdidload recommended to use in favor of cell reuse, the above method in the cell processing data too much scrolling is not smooth//-(void) Registern    IB: (uinib *) nib Forcellreuseidentifier: (NSString *) identifier Ns_available_ios (5_0);    -(void) RegisterClass: (Class) Cellclass forcellreuseidentifier: (NSString *) identifier Ns_available_ios (6_0);    /** Register A *//** static NSString *customcellidentifier = @ "Customcellidentifier";    static BOOL nibsregistered = NO; if (!nibsregistered) {[TableView registernib:[uinib nibwithnibname:@ "Customcellxib" Bundle:nil] Forcellreuseidenti        Fier:customcellidentifier];    nibsregistered = YES; } UITableViewCell *cell = [tableView Dequeuereusablecellwithidentifier:customcellidentifier];            *//Register different list styles if (!nibsregistered) {if (indexpath.row% 2 = = 0) {type = 0;        [_mtableview registernib:[uinib nibwithnibname:@ "Cell1" Bundle:nil] forcellreuseidentifier:cellidf1];            }else{type = 1;        [_mtableview Registerclass:[tableviewcell class] forcellreuseidentifier:cellidf2];    } nibsregistered = YES;    } UITableViewCell *cell;        if (type = = 0) {cell = [TableView DEQUEUEREUSABLECELLWITHIDENTIFIER:CELLIDF1];        [Cell Setbackgroundcolor:[uicolor Redcolor]];    [[Cell Textlabel] settext:@ "CELLL1"];        } if (type = = 1) {cell = [TableView DEQUEUEREUSABLECELLWITHIDENTIFIER:CELLIDF2];        [Cell Setbackgroundcolor:[uicolor Greencolor]];    [[Cell Textlabel] settext:@ "Cell2"]; } return cell;    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreAted.}    -(Ibaction) Segaction: (Uisegmentedcontrol *) Sender {number = 10;            Switch (sender.selectedsegmentindex) {case 0:type = 0;            [_mtableview registernib:[uinib nibwithnibname:@ "Cell1" Bundle:nil] forcellreuseidentifier:cellidf1];        Break            Case 1:type = 1;            [_mtableview Registerclass:[tableviewcell class] forcellreuseidentifier:cellidf2];        Break    Default:break; } [_mtableview reloaddata];} @end


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

IOS UITableView Usage Summary

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.