天天看点

UITableView 及其传值

准备工作

@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:(, , self.view.frame.size.width, self.view.frame.size.height - ) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:tableView];

// 设置行高

tableView.rowHeight = ;

// 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的特点
     只初始化一次
     如果没有初始值, 默认是零
     知道程序结束, 才会消失
    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 ;
}

- (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(, , self.view.frame.size.width, self.view.frame.size.height - ) style:UITableViewStylePlain];

tableview.blackgroundColor = [UIclor grayColor];
[self.view addSubview:tableview];
[tableview release];
tableview.rowHeight = ;
// 设置代理人
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 ;
}

- (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];
}