UI9_UITableView练习

2015-12-17 20:25:00

//
// RootViewController.h
// UI9_UITableView练习
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


//
// RootViewController.m
// UI9_UITableView练习
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "RootViewController.h"

import "SecondViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, PassDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)NSIndexPath *index;// 记录点击的cell位置
@end

@implementation RootViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //
    self.title = @"YIem";
    //
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    

// _tableView.backgroundColor = [UIColor yellowColor];

[self.view addSubview:_tableView];
[_tableView release];


_tableView.delegate = self;
_tableView.dataSource = self;


// 数据处理
self.dataArr = [@[@"大娃", @"二娃", @"三娃", @"四娃", @"五娃", @"六娃", @"七娃"]mutableCopy];

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    // 通过数组元素个数 动态设置cell行数
    return _dataArr.count;
    }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {

    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];

    }

    // 通过行数从数组中获取对应下标的元素
    cell.textLabel.text = _dataArr[indexPath.row];
    return cell;
    }

// 点击cell

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
    {

    SecondViewController *secVC = [[SecondViewController alloc]init];
    // 传值
    secVC.name  = _dataArr[indexPath.row];
    // 协议传值
    secVC.delegate = self;
    // 记录点击cell的位置
    self.index = indexPath;
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];

    }

  • (void)passValue:(NSString *)name
    {

    // 修改信息
    // 通过记录的点击位置信息 将传递过来的值 替换原内容
    [_dataArr replaceObjectAtIndex:_index.row withObject:name];
    // tableView整体刷新 重新执行所有协议方法

    // [_tableView reloadData];

    // 单行刷新
    // 参数1: 需要刷新的下标
    // 参数2: 刷新动画
    [_tableView reloadRowsAtIndexPaths:@[_index] withRowAnimation:UITableViewRowAnimationRight];
    

    }

  • (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



//
// SecondViewController.h
// UI9_UITableView练习
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@protocol PassDelegate <NSObject>

  • (void)passValue:(NSString *)name;

@end
@interface SecondViewController : UIViewController

@property (nonatomic, copy)NSString *name;

@property (nonatomic, assign) id<PassDelegate>delegate;

@end


//
// SecondViewController.m
// UI9_UITableView练习
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    _textField.backgroundColor = [UIColor yellowColor];
    _textField.text = self.name;
    [self.view addSubview:_textField];
    [_textField release];
    // buttomn
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button release];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    

    }

  • (void)back
    {

    
    [self.delegate passValue:_textField.text];
    [self.navigationController popViewControllerAnimated:YES];

    }

  • (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
//
// SecondViewController.m
// UI9_UITableView练习
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    _textField.backgroundColor = [UIColor yellowColor];
    _textField.text = self.name;
    [self.view addSubview:_textField];
    [_textField release];
    // buttomn
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button release];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    

    }

  • (void)back
    {

    
    [self.delegate passValue:_textField.text];
    [self.navigationController popViewControllerAnimated:YES];

    }

  • (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

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »