2012年11月3日 星期六

[ iOS ] 兩個TableView相互切換

1.執行結果如下;

2.DLIViewController.h
//
//  DLIViewController.h
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import

@interface DLIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *listData;
@end


3.DLIViewController.m

//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"
#import "DLIViewController2.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"nkut_logo.jpg"];
    cell.imageView.image = cellImage;
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLIViewController2 *controller = [[DLIViewController2 alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}
@end

4. DLIViewController2.h
//
//  DLIViewController2.h
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import

@interface DLIViewController2 : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView2;
@property (strong, nonatomic)NSArray *listData2;
@end

5. DLIViewController2.m
//  DLIViewController2.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"
#import "DLIViewController2.h"

@interface DLIViewController2 ()

@end

@implementation DLIViewController2
@synthesize listData2;
- (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 from its nib.
    listData2 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData2 count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData2 objectAtIndex:indexPath.row];

    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.textLabel.text = [listData2 objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLIViewController *controller = [[DLIViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}

@end

沒有留言:

張貼留言