博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。...
阅读量:4309 次
发布时间:2019-06-06

本文共 6686 字,大约阅读时间需要 22 分钟。

一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import 
@interface RootViewController : UIViewController
{ 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 
@interface DetailViewController : UIViewController
{ 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

 

 

 
 

转载于:https://www.cnblogs.com/yang-guang-girl/p/5252900.html

你可能感兴趣的文章
openstack报错解决二
查看>>
linux source命令
查看>>
openstack报错解决三
查看>>
乙未年年终总结
查看>>
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>