您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 頁面傳值實戰:TableView 與詳情頁的雙向資料傳遞

編輯:iOS 開發綜合

列表頁點選一筆資料跳到詳情頁,詳情頁新增一筆資料後返回列表要能即時刷新——這是 App 開發中最基礎也最常見的資料流需求。本文示範正向傳值(列表 → 詳情)與反向回傳(詳情 → 列表)兩種做法。

正向傳值:把選中項帶到下一頁

#import "MainViewController.h"
#import "SecondViewController.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *imageView;
@end

@implementation MainViewController

- (void)dealloc {
    [_arr release];
    [_imageView release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"盧俊義", @"吳用", @"公孫勝",
                    @"關勝", @"林沖", @"秦明", @"呼延灼", @"花容", @"柴進",
                    @"李應", @"朱仝", @"魯智深", @"武松", nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"表視圖";

    self.tableView = [[UITableView alloc]
        initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.rowHeight = 50;
    self.tableView.dataSource = self;
    self.tableView.delegate   = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];

    // 頂部懸浮圖片
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h1.jpeg"]];
    self.imageView.frame = CGRectMake(0, -200, self.view.frame.size.width, 200);
    [self.tableView addSubview:self.imageView];
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
}

下拉時的視差效果

UITableView 的 delegate 已預先符合 UIScrollView 的協議,只要設定了 delegate,就能直接實作滾動回呼。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    if (y < 0) {
        // 往下拉時讓圖片跟著變高,形成視差
        self.imageView.frame = CGRectMake(0, y, self.view.frame.size.width, -y);
    }
}

資料來源與點擊跳轉

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text       = self.arr[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.section];
    cell.imageView.image      = [UIImage imageNamed:@"天平.png"];
    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secVC = [[SecondViewController alloc] init];
    secVC.name = self.arr[indexPath.row];        // 正向傳值
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
}

反向傳值:定義通訊協議

在 SecondViewController 的標頭檔中宣告一個協議,讓列表頁有機會實作它。

// SecondViewController.h
#import <UIKit/UIKit.h>

@protocol SecondViewControllerDelegate <NSObject>
- (void)changeValue:(NSString *)value;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, copy)   NSString *name;
@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@end

列表頁實作該協議,把新資料塞進陣列再刷新表格:

- (void)changeValue:(NSString *)value {
    [self.arr addObject:value];
    [self.tableView reloadData];
}

詳情頁的實作

// SecondViewController.m
@interface SecondViewController ()
@property (nonatomic, retain) UILabel     *label;
@property (nonatomic, retain) UITextField *textfield;
@property (nonatomic, retain) UIButton    *button;
@end

@implementation SecondViewController

- (void)dealloc {
    [_label release]; [_textfield release]; [_button release]; [_name release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
    self.label.backgroundColor = [UIColor redColor];
    self.label.layer.borderWidth = 1;
    self.label.layer.cornerRadius = 10;
    self.label.text = self.name;      // 接收上一頁傳來的值
    [self.view addSubview:self.label];
    [self.label release];

    self.textfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 40)];
    self.textfield.layer.borderWidth = 1;
    self.textfield.layer.cornerRadius = 10;
    [self.view addSubview:self.textfield];
    [self.textfield release];

    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(200, 300, 150, 30);
    [self.button setTitle:@"返回" forState:UIControlStateNormal];
    self.button.layer.borderWidth = 1;
    self.button.layer.cornerRadius = 5;
    [self.button addTarget:self action:@selector(click:)
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}

- (void)click:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(changeValue:)]) {
        [self.delegate changeValue:self.textfield.text];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end

別忘了在列表頁把 delegate 指給自己:secVC.delegate = self;。實務上有兩種常見寫法:delegate 搭配協程回呼,或用 block 取代協議(下一篇會示範)。delegate 的優點是關係清楚、可擴充多個方法;block 則適合只有單一回呼的輕量情境。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved