UITableView小知识点

特别注意一点:对于一些异步请求的方法向cell中添加数据以及图片时,要记得刷表
UITableViewCell右边带箭头的附件按钮添加

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

给UITableView添加自定义的按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"head1.jpg"] forState:UIControlStateNormal];
    button.frame = CGRectMake(270, 10, 20, 20);
    button.center = CGPointMake(280, cell.bounds.size.height/2);
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = button;
    ```
    如果cell的某个区的某行的高度单独不一样时的设置
    ```

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //0组0行返回100
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 100;
    }
    return 60;
}

//点击系统的附件按钮时执行的方法

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    ZKDetailViewController *detail = [[ZKDetailViewController alloc]init];

    [self.navigationController pushViewController:detail animated:YES];
}

//选中当前行的时候执行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    ZKDetailViewController *detail = [[ZKDetailViewController alloc]init];
//    
//    [self.navigationController pushViewController:detail animated:YES];
}

给cell添加背景图片(注意:下面的方法是按照5像素进行平铺)

    UIImage *backImage = [[UIImage imageNamed:@"table_cell_bg"]stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    UIImageView *backView = [[UIImageView alloc]initWithImage:backImage];
    cell.backgroundView = backView;
    ```
    添加表头表尾的方法
    ```
    //1.创建表头
    UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    headerLabel.text = @"这里是表头";
    headerLabel.textAlignment = NSTextAlignmentCenter;
    headerLabel.backgroundColor = [UIColor redColor];
    _tableView.tableHeaderView = headerLabel;

    //表尾
    UILabel *footerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    footerLabel.text = @"这里是表尾";
    footerLabel.textAlignment = NSTextAlignmentCenter;
    footerLabel.backgroundColor = [UIColor redColor];
    _tableView.tableFooterView = footerLabel;

添加组头组尾的方法:分为title方法和ivew方法
title方法:

//添加组头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _sectionheaderTitles[section];
}
//添加组尾
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return _sectionFooterDetails[section];
}

view方法:(通常情况下,都是用view方法来设置组头组尾)(注意:当title与view两个方法通过设置时,优先执行view方法)

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *header = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    header.backgroundColor = [UIColor greenColor];
    header.textAlignment = NSTextAlignmentLeft;
    header.text = _sectionheaderTitles[section];

    return header;
}

特别注意不要忘了自定义高度的定义
`-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section`

//添加组尾视图

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UILabel *footer = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    footer.backgroundColor = [UIColor greenColor];
    footer.textAlignment = NSTextAlignmentLeft;
    footer.text = _sectionFooterDetails[section];

    return footer;
}

//注意:组头,组尾的高度,要通过专门的方法实现 (自定义cell时,高度也要记得单独设置)

//自定义组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
//自定义组尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

TableView不显示没内容的Cell怎么办?

self.tableView.tableFooterView = [[UIView alloc] init];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值