【代碼筆記】點擊城市中的tableView跳轉到旅遊景點的tableView,下面會有“顯示”更多。,tableview跳轉

來源:互聯網
上載者:User

【代碼筆記】點擊城市中的tableView跳轉到旅遊景點的tableView,下面會有“顯示”更多。,tableview跳轉

一,。

二,工程圖。

三,代碼。

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

 

 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.