UITableView 及其传值

来源:互联网 发布:chart.js 提示文字 编辑:程序博客网 时间:2024/06/06 05:21

准备工作

@property(nonatomic, tetain)NSMutableArray *arr// 自定义初始化- (instancetype)initWithNibName:(NSString *)nibNameOrNil  bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNile];    if(self){        self.arr = [NSMutableArray arrayWithObject:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];    }    return self;}self.view.backgroundColor = [UIColor whiteColor];self.navigationController.navigationBar.translucent = NO;self.title = @"表视图";UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake:(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];tableView.backgroundColor = [UIColor yellowColor];[self.view addSubview:tableView];// 设置行高tableView.rowHeight = 100;// tableView 的两套代理方法tableView.dataSource = self; // 有两个必须实现的方法:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;tableView.delegate = self;// 第一套协议方法- (NSInterger)tableview:(UITableView *)tableView numberOfRowInSection:(NSInteger)section{    // 让数组里的元素个数与行数保持一致    // 先执行设置分区的方法, 后执行每区有多少行    return self.arr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 创建cell    // static的特点    1. 只初始化一次    2. 如果没有初始值, 默认是零    3. 知道程序结束, 才会消失    static NSString *reuse = @"reuse";    // 当cell显示结束之后,会把cell统一fangdao重用池中, 等需要cell显示了,先从重用池中找, 看有没有闲置的cell, 如果有的话就用闲置的cell, 如果没有在创建    // cell的重用目的是为了节约创建的成本, 拥有有限的cell把所有的数据显示出来    // 给重用池先设置一个重用的标志,根据这个标志可以找到重用池    // tableView通过重用的标志在重用池中寻找cell, 如果有闲置的cell, cell会保存一个有效的cell对象地址, 如果没有, cell里面则是nil, 空    UITableViewCell *cell = [tableView     dequeueReusableCellWithIndentifier:reuse];    if(!cell){        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];    }    // 对cell 进行赋值 cell里有三个默认的控件    cell.textLable.text = self.arr[indexPath.row];    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];    cell.imageView.image = [UIImage imageNamed:@"图片名"];    return cell;}- (NSInteger)numberOfSectionsInTableView;(UITableView *)tableView{    // 设置显示的区数    return 10;}- (Nsstring *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    // 显示每个区的头标题    return @"lishanshan";}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableview{    // 快速找个 区    return @[@"0", @"2"];}// 第二套协议方法- (void)tableView:(UITableView *)tableVIew didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 点击indexPath 所执行的方法}

UITableView的界面传值

// 从前往后传@property(nonatomic, retain)UITableView *tableview;@property(nonatomic, retain)NSMutableArray *arr;@property(nonatomic, retain)UIImageView *imageView;self.view.backgroundColor = [UIColor greenColor];self.navigationController.navigationBar.translucent = NO;self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];self.title = @"视图组";UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];tableview.blackgroundColor = [UIclor grayColor];[self.view addSubview:tableview];[tableview release];tableview.rowHeight = 100;// 设置代理人tableview.dataSource = self;tableview.delegate = self; self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"图片名"];方法- (NSinteger)tableView:(UITableView *)tableView numberOfRowsInsection:(NSInteger)section{    return self.arr.count;}- (NSInteger)numberOfsectionsIntableView:(UITableView *)table{    return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSSting *reuse = @"reuse";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];    if (!cell) {        cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIndentifier:resuse] autorelease];    }    cell.textlable.text = self.arr[indexPath.row];    cell.detailTextLabel.text = [NSString StringWithFormat:@"%ld", indexPath.section];    cell.imageView.image = [UIImage imageNamed:@"图片名"];    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    SecondViewController *secondAC = [[SecondViewController alloc] init];    [self.navigationController pushViewController:secondAC animated:YES];    SecondAC.str = self.arr[indexPath.row];}
0 0
原创粉丝点击