TableView iOS

view + tableview 需要手动拖拽增加datasource和delegate 否则使用tableviewcontroller比较方便

1.显示tableviewcell

image text detail text

UITableViewCellStyleDefault 显示image + text

UITableViewCellStyleSubtitle 显示image + text + (换行)detail text

UITableViewCellStyleValue1 显示image + text + detail text

UITableViewCellStyleValue2 显示text + detail text

如果这些内置的cell style无法满足,可以重新定义table view cell


2.cellForRowAtIndexpath

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; 
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

前者MRC,后者ARC

具体使用方式不同之处在于

1.需要判断是否返回的cell为nil,如果为nil,需要做相应的处理

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
 SimpleTableIdentifier];
 if (cell == nil) {
 cell = [[UITableViewCell alloc]
 initWithStyle:UITableViewCellStyleDefault
 reuseIdentifier:SimpleTableIdentifier];
 }
2.则将if判断cell是否为nil省略,但是需要提前注册一个cell的class

一般可以在ViewDidLoad中注册

UITableView *tableView = (id)[self.view viewWithTag:1];
 [tableView registerClass:[BIDNameAndColorCell class]
 forCellReuseIdentifier:CellTableIdentifier];
后续就可以直接使用了

BIDNameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:
 CellTableIdentifier
 forIndexPath:indexPath];
3.- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

这个函数主要定义了cell中view的呈现形式


3.设置inset,屏蔽掉电源部分,以及滚动时,遮住电源部分

UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = 20;
    [tableView setContentInset:contentInset];
        
    UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    barBackground.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];
    [self.view addSubview:barBackground];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值