UITableView(表视图)

1.UITableView

UITableView继承于UIScrollView,表视图的每一条数据都显示在UITableViewCell对象中,表视图可以分区显示数据,每个分区成为一个section,每一行为row,编号都是从零开始.

2.表视图的创建

 

<span style="font-size:18px;">//初始化一个UITableView
    self.tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStyleGrouped];
    //separatorStyle:表视图的分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //表视图分割线的颜色
    self.tableView.separatorColor = [UIColor redColor];
    //rowHeight:表视图的行高(设置行高一般使用协议中的方法)
    self.tableView.rowHeight = 100;
</span>

3.表视图显示

3.1表视图显示需要遵循两个协议,即UITableViewDataSource和UITableViewDelegate这两个协议.

其中在UITableViewDataSource中有了两个必须实现的方法.

第一个必须实现的方法.

<pre name="code" class="objc"><span style="font-size:18px;">//设置(每一个)section中row的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
</span>

 
第二个必须实现的方法. 

<span style="font-size:18px;">//显示每一行的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     //初始化一个UITableViewCell()
    self.tableCell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
    
    if (nil == self.tableCell) {
        self.tableCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"];
    }
    
    self.tableCell.textLabel.text = [self.array3[indexPath.section][indexPath.row]valueForKey:@"name"];
    
    self.tableCell.detailTextLabel.text = [self.array3[indexPath.section][indexPath.row]valueForKey:@"Tel"];
    
    self.tableCell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[self.array3[indexPath.section][indexPath.row]valueForKey:@"image"]]];
    
    
   
    
    self.tableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
//    self.tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return self.tableCell;
}
</span>

以下的方法为常用方法(不是一定要实现)

<span style="font-size:18px;">//设置UItableView的分组数目
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}</span>
<span style="font-size:18px;">//重新加载数据(重新加载数据调用reloadData方法)
-(void)viewDidDisappear:(BOOL)animated{
    [self.rootView.tableView reloadData];
}</span>
<span style="font-size:18px;">
//实现跳转和传值
</span><pre name="code" class="objc"><span style="font-size:18px;">-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    _detailVC.image = self.tableCell.imageView.image;
    _detailVC.stringName = self.tableCell.textLabel.text;
    _detailVC.stringTel = self.tableCell.detailTextLabel.text;
    
    [self.navigationController pushViewController:self.detailVC animated:YES];
}</span>

<span style="font-size:18px;">//设置cell的header的距离
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

//向UITableViewCell的header中添加视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:tableView.tableHeaderView.frame];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(222, 20, 100, 20)];
    label.text = @"Cocoa";
    [view addSubview:label];
    
    if (section == 0) {
        return view;
    }else{
         return 0;
    }
}

//向UITableViewCell中的foot中添加视图
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return 0;

}
</span>


 
4.表视图的重用机制 

UITableView是靠mutableSet来实现重用功能

出屏幕的cell会被添加到mutableSet中,进屏幕的cell,先从set中获取,如果获取不到,才创建一个cell.在cell显示之前,给cell赋上相应的内容.cell的reusableIdentify是重用的关键.


//UITableViewCell的重用
   self.tableCell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
    
    if (nil == self.tableCell) {
        self.tableCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"];
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值