iOS开发系列 ---- UI (自定义TableView)
程序员文章站
2022-03-25 07:57:14
ios开发系列 ---- ui (自定义tableview)。本章实现效果:
我们使用mvc架构来实现自定义tableview,具体的mvc介绍请自行百度,在本章中不作为主要内容讲解。
model...
ios开发系列 ---- ui (自定义tableview)。本章实现效果:
我们使用mvc架构来实现自定义tableview,具体的mvc介绍请自行百度,在本章中不作为主要内容讲解。
model层:
datasource.h
#import @interface datasource : nsobject + (nsarray *)getuserinfo; @end
datasource.m
#import "datasource.h" @implementation datasource + (nsarray *)getuserinfo { nsstring * path = [[nsbundle mainbundle] pathforresource:@"users" oftype:@"plist"]; nsarray * arrayreturn = [nsarray arraywithcontentsoffile:path]; return arrayreturn; } @end
view层:
customtableviewcell.h
#import @interface customtableviewcell : uitableviewcell @property (nonatomic, strong) uilabel * labelname;//姓名 @property (nonatomic, strong) uilabel * labelnum; //手机号 @end
customtableviewcell.m
#import "customtableviewcell.h" @implementation customtableviewcell //重写初始化方法:将控件添加到单元格上,如果将子视图控件添加到cell上 借助contenview视图,这样的话cell上子视图会随着cell的变化而变化 - (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { if (self = [super initwithstyle:style reuseidentifier:reuseidentifier]) { self.labelname = [[uilabel alloc] initwithframe:cgrectmake(100, 0, self.bounds.size.width-200, 40)]; self.labelname.textcolor = [uicolor cyancolor]; self.labelname.textalignment = nstextalignmentcenter; [self.contentview addsubview:self.labelname]; self.labelnum = [[uilabel alloc] initwithframe:cgrectmake(100, 40, self.bounds.size.width-200, 40)]; self.labelnum.textcolor = [uicolor greencolor]; self.labelnum.textalignment = nstextalignmentcenter; [self.contentview addsubview:self.labelnum]; } return self; } - (void)awakefromnib { [super awakefromnib]; } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; } @end
viewcontroller层:
viewcontroller.m
#import "viewcontroller.h" #import "datasource.h" #import "customtableviewcell.h" #import "twoviewcontroller.h" @interface viewcontroller ()< uitableviewdatasource, uitableviewdelegate> @property (nonatomic, strong) nsmutablearray *arrayds; @property (nonatomic, strong) uitableview *tableview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; [self setupdatas]; [self setupsubviews]; } - (void)setupdatas { //从model层拿到数据 nsarray * array = [datasource getuserinfo]; //把拿到的数据给我们的数据源 self.arrayds = [[nsmutablearray alloc] initwitharray:array]; self.navigationitem.title = @"自定义单元格"; } - (void)setupsubviews { self.automaticallyadjustsscrollviewinsets = no; self.tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:uitableviewstyleplain]; self.tableview.delegate = self; self.tableview.datasource = self; [self.view addsubview:self.tableview]; uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50)]; view.backgroundcolor = [uicolor redcolor]; [self.view insertsubview:view abovesubview:self.tableview]; } #pragma mark - uitableviewdatasource - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return self.arrayds.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring * str = nil; if ([[[self.arrayds objectatindex:indexpath.row] objectforkey:@"phonenum"] hasprefix:@"100"]) { str = @"customcell"; customtableviewcell * cell = [tableview dequeuereusablecellwithidentifier:str]; if (cell == nil) { cell = [[customtableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:str]; } cell.labelname.text = [[self.arrayds objectatindex:indexpath.row] objectforkey:@"personname"]; cell.labelnum.text = [[self.arrayds objectatindex:indexpath.row] objectforkey:@"phonenum"]; return cell; } else { str = @"systemcell"; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:str]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:str]; } cell.textlabel.text = [[self.arrayds objectatindex:indexpath.row] objectforkey:@"phonenum"]; return cell; } } - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [self.tableview reloaddata]; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { twoviewcontroller *twovc = [[twoviewcontroller alloc] init]; [self.navigationcontroller pushviewcontroller:twovc animated:yes]; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 100; } @end