IOS开发之tableView点击行跳转并带有“显示”更多功能
程序员文章站
2023-10-19 10:16:17
首先给大家展示下效果图,觉得还满意的话,请继续学习代码实现过程。
一,工程图。
二,代码。
rootviewcontroller.h...
首先给大家展示下效果图,觉得还满意的话,请继续学习代码实现过程。
一,工程图。
二,代码。
rootviewcontroller.h
#import <uikit/uikit.h> @interface rootviewcontroller : uiviewcontroller <uitableviewdelegate,uitableviewdatasource> { uitableview * _tableview; nsmutablearray * provincearray; } @end rootviewcontroller.m #import "rootviewcontroller.h" //详细页面 #import "detailviewcontroller.h" @interface rootviewcontroller () @end @implementation rootviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. provincearray = [[nsmutablearray alloc] initwithobjects:@"北京", @"上海", @"云南",@"四川",@"海南", @"江苏", @"香港", @"澳门", @"*", nil]; _tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 0, 320, 380) style:uitableviewstylegrouped]; _tableview.delegate = self; _tableview.datasource = self; [self.view addsubview:_tableview]; } #pragma -mark -uitableviewdelegate - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell* cell = [tableview dequeuereusablecellwithidentifier:@"id"]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:@"id"] ; } cell.textlabel.text = [provincearray objectatindex:indexpath.row]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; return cell; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 9; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 60; } //点击进入下一页 - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { detailviewcontroller* svc = [[detailviewcontroller alloc] init]; svc.title = [nsstring stringwithformat:@"%@",[provincearray objectatindex:indexpath.row]]; [self.navigationcontroller pushviewcontroller:svc animated:no]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } /* #pragma mark - navigation // in a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // get the new view controller using [segue destinationviewcontroller]. // pass the selected object to the new view controller. } */ @end
detailviewcontroller.h
#import <uikit/uikit.h> @interface detailviewcontroller : uiviewcontroller <uitableviewdelegate,uitableviewdatasource> { uitableview* _tableview; nsarray* provincearr; nsarray* cityarray; nsstring* cityname; nsmutablearray* ditailname; nsstring* ditialplacename; nsdictionary *dicforplist; } @end
detailviewcontroller.m
#import "detailviewcontroller.h" static int rownumber; @interface detailviewcontroller () @end @implementation detailviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. //传过来的城市名字 cityname = self.title; //tableview显示行数 rownumber = 20; //tableview _tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 0, 320, 460 ) style:uitableviewstylegrouped]; _tableview.delegate = self; _tableview.datasource = self; [self.view addsubview:_tableview]; nsmutablearray* citycomparearr = [[nsmutablearray alloc] init]; ditailname = [[nsmutablearray alloc] init]; //城市的plist文件 dicforplist = [nsdictionary dictionarywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"cityname" oftype:@"plist"]]; //北京所有数据 provincearr = [dicforplist objectforkey:cityname]; for (int j = 0; j < [provincearr count]; j++) {//遍历省的所有数据 cityarray = [provincearr objectatindex:j];//取出每一个小数组 for (int i = 0; i < [cityarray count]; i++) {//遍历小数组 nsstring* strstr = [cityarray objectatindex:i]; //得到小数组内容 if ([strstr isequaltostring:cityname] && j + 1 < [provincearr count]) { citycomparearr = [provincearr objectatindex:j + 1]; if (![[cityarray objectatindex:i + 1] isequaltostring:[citycomparearr objectatindex:i + 1]]) { [ditailname addobject:[cityarray objectatindex:i + 1]]; } else { } } if ([strstr isequaltostring:cityname] && j + 1 == [provincearr count]){ nslog(@"last one?"); } } } } #pragma -mark -uitableviewdelegate - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell* cell = [tableview dequeuereusablecellwithidentifier:@"id"]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:@"id"]; } if (indexpath.section == 0) { cell.textlabel.text = [ditailname objectatindex:indexpath.row]; } if (indexpath.section == 1) { cell.textlabel.text = @"显示更多"; cell.textlabel.textalignment = nstextalignmentcenter; } return cell; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section == 0) { if (rownumber > [ditailname count]) {//显示到最多的时候 return [ditailname count]; } return rownumber; } else return 1; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 1) { rownumber += 20; [tableview reloaddata]; } else { nslog(@"---跳转到另外一个页面----"); } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } /* #pragma mark - navigation // in a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // get the new view controller using [segue destinationviewcontroller]. // pass the selected object to the new view controller. } */ @end
以上内容是小编给大家介绍的ios开发之tableview点击行跳转并带有“显示”更多功能,有问题欢迎各位给我留言,我会及时和大家取得联系的,同时感谢大家一直以来对网站的支持。
上一篇: 睡觉前喝牛奶真有好处吗?
下一篇: 水芹是芹菜吗,今天告诉你