IOS开发之tableView点击行跳转并带有“显示”更多功能

时间:2022-05-07 14:41:09

首先给大家展示下效果图,觉得还满意的话,请继续学习代码实现过程。

IOS开发之tableView点击行跳转并带有“显示”更多功能

一,工程图。

IOS开发之tableView点击行跳转并带有“显示”更多功能

二,代码。

rootviewcontroller.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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点击行跳转并带有“显示”更多功能,有问题欢迎各位给我留言,我会及时和大家取得联系的,同时感谢大家一直以来对服务器之家网站的支持。