IOS学习(九)UITabView使用及几种常见的创建方式
程序员文章站
2022-05-04 09:25:24
1. 纯代码创建
- (void)viewdidload {
[super viewdidload];
// do any additional setup after lo...
1. 纯代码创建
- (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. self.view.backgroundcolor = [uicolor graycolor]; //style:uitableviewstylegrouped 分组 //style:uitableviewstyleplain 当组名称滑动到顶部时,将固定在顶部位置 uitableview *tabview = [[uitableview alloc] initwithframe:self.view.frame style:uitableviewstyleplain]; tabview.backgroundcolor = [uicolor yellowcolor]; tabview.datasource = self; [self.view addsubview:tabview]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview{ return 5; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section{ return [nsstring stringwithformat:@"分组%zd", section]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ return 6; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ uitableviewcell *cell = [[uitableviewcell alloc] initwithframe:cgrectmake(0, 0, cgrectgetwidth(self.view.bounds), 50)]; cell.textlabel.text = [nsstring stringwithformat:@"hello%zd", indexpath.row]; return cell; }
2. 创建viewcontroller时,同时创建.xlb文件,再把uitabview拖拽到.xlb文件中
#import "secondviewcontroller.h" @interface secondviewcontroller () @property (weak, nonatomic) iboutlet uitableview *tabview; @end @implementation secondviewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view from its nib. self.tabview.backgroundcolor = [uicolor greencolor]; }
3.自定义tabview,需创建一个。xlb文件
/ // customtabview.h // uitabviewtest // // created by zzj on 2016/11/29. // copyright © 2016年 zzj. all rights reserved. // #import @interface customtabview : uitableview + (instancetype) tableviewwithdatasource:(id) datasource; @end
#import "customtabview.h" @implementation customtabview + (instancetype)tableviewwithdatasource:(id)datasource{ customtabview *customtabview = [[[nsbundle mainbundle] loadnibnamed:@"customtabvew" owner:self options:nil] lastobject ]; customtabview.datasource = datasource; return customtabview; }
#import "thirdviewcontroller.h" #import "customtabview.h" @interface thirdviewcontroller () @end @implementation thirdviewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. customtabview * tabview = [customtabview tableviewwithdatasource:self]; [self.view addsubview:tabview]; }
xlb文件中只有一个 uitabview控件
上一篇: 几月份的西瓜最便宜?西瓜都有什么品种呢?
下一篇: jQuery登陆判断简单实现代码