[IOS Development] TableView, multiple Tableviewcell, custom cell, cell painting (Storyboard + code mode)

Source: Internet
Author: User
Tags uikit

The first step:

// UserTableViewCell.h here defines the first cell#import <UIKit/UIKit.h>**userviewcellname;@ End//UserTableViewCell2.h define the second cell here,#import <UIKit/UIKit.h>** userviewcell2image; @end

The TableView in the storyboard and the cell's class and cell identifier don't say a word.

2, designed these things, and began to enter the Tableviewcontroller://UserTableViewController.h#import <UIKit/UIKit.h>@interface Usertableviewcontroller:uitableviewcontroller@property (nonatomic,strong) nsdictionary*Userviewcelldic, @property (nonatomic, strong) Nsarray*listgroupname; @end//USERTABLEVIEWCONTROLLER.M#import "UserTableViewController.h" #import "UserTableViewCell.h" #import "UserTableViewCell2.h" @interface Usertableviewcontroller () @end @implementation Usertableviewcontroller- (void) Viewdidload{[super Viewdidload];self.tableview.separatorstyle= Uitableviewcellaccessorynone;//Remove Split LineNSBundle *bundle =[NSBundle Mainbundle]; NSString*plistpath =[Bundle pathforresource:@ "Userviewcell" oftype:@ "plist"];self. Userviewcelldic=[[Nsdictionary alloc]initwithcontentsoffile:plistpath];self.listgroupname=[Self.  Userviewcelldic AllKeys]; Self.listgroupname= [Self.listgroupname sortedarrayusingselector: @selector (compare:)];//Sort}-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView//returns a few sections{return[Self.listgroupname Count];}-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section//Section header name is set to null{nsstring*groupname =@ "";returnGroupName;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section//number of rows per section{nsstring*groupname =[Self.listgroupname objectatindex:section]; Nsarray*listitem =[Self. Userviewcelldic Objectforkey:groupname];return[ListItem count];}#pragmamark–tableviewdatasource-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *userviewcellidentifier =@ "Usertableviewcell";StaticNSString *userviewcellidentifier2 =@ "UserTableViewCell2"; Nsuinteger Section=[Indexpath section]; Nsuinteger Row=[Indexpath Row]; NSString*groupname =[Self.listgroupname objectatindex:section]; Nsarray*listitem =[Self. Userviewcelldic Objectforkey:groupname]; Nsdictionary*rowdict =[ListItem Objectatindex:row];if(0==Section ) {UserTableViewCell2*cell =[TableView Dequeuereusablecellwithidentifier:userviewcellidentifier2];cell.name.text=[rowdict objectforkey:@ "name"]; NSString*imagepath =[rowdict objectforkey:@ "image"];imagepath=[ImagePath stringbyappendingstring:@ ". png"];cell.userviewcell2image.image=[UIImage Imagenamed:imagepath];cell.accessorytype= Uitableviewcellaccessorydisclosureindicator;//the last arrow//Draw LineUIView *view = [[UIView alloc]initwithframe:cgrectmake (0, -, the,1)]; UIView*leftview = [[UIView alloc]initwithframe:cgrectmake (0,0,3, the)]; UIView*rightview = [[UIView alloc]initwithframe:cgrectmake (317,0,3, the)];view.backgroundcolor= [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha1];leftview.backgroundcolor= [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha1];rightview.backgroundcolor= [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha1]; [Cell.contentview Addsubview:view]; [Cell.contentview Addsubview:leftview]; [Cell.contentview Addsubview:rightview];returncell;}Else{Usertableviewcell*cell =[TableView Dequeuereusablecellwithidentifier:userviewcellidentifier];cell.userviewcellname.text=[rowdict objectforkey:@ "name"]; NSString*imagepath =[rowdict objectforkey:@ "image"];imagepath=[ImagePath stringbyappendingstring:@ ". png"];cell.userviewcellicon.image=[UIImage Imagenamed:imagepath];cell.accessorytype= Uitableviewcellaccessorydisclosureindicator;//the last arrow//Draw LineUIView *view = [[UIView alloc]initwithframe:cgrectmake (0, the, the,1)]; UIView*leftview = [[UIView alloc]initwithframe:cgrectmake (0,0,3, +)]; UIView*rightview = [[UIView alloc]initwithframe:cgrectmake (317,0,3, +)];view.backgroundcolor= [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha1];//Alpha is transparencyLeftview.backgroundcolor = [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha0.3];rightview.backgroundcolor= [Uicolor colorwithred: +/255.0fGreen +/255.0fBlue233/255.0fAlpha1]; [Cell.contentview Addsubview:view]; [Cell.contentview Addsubview:leftview]; [Cell.contentview Addsubview:rightview];returncell;}}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath//the height of the cell{nsuinteger section=[Indexpath section];if(0==Section ) {return  the;}Else{return  +;}}

The plist file is attached here:

3. Operation Effect:

[IOS Development] TableView, multiple Tableviewcell, custom cell, cell painting (Storyboard + code mode)

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.