UITableView 学习整理

最近项目中,有一个页面需要用到TableView,于是借着这个机会好好把TableView这块整理了一番。

基本用法

TableView 基本上大家都应该在自己的项目中使用过,其基本用法,这里也不过多做介绍,网上相关文章非常多。这里列出几个自己刚刚学习TableView时所借鉴过的文章。

iOS开发系列-UITableView全面解析

iOS Programming 101: Adding Section and Index List in UITableView

高级用法
  1. 对于TableView,如何做到点击表格时,显示点击效果;点击完毕后,效果回复?

    只需要在实现UITableView的以下代理,并在代理里面做如下设置即可。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    
  2. 如何修改UITableViewCell中UIImageView的位置和大小?

    有时候我们为了保证界面的美观,希望能够修改UITableViewCell中图片的大小和位置。可是,当我们通过cell.imageView.bounds、cell.imageView.frame设置时,根本无法生效。所以,我们只能自定义一个UITableViewCell并且重写layoutSubviews方法:

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        self.imageView.bounds = CGRectMake(0.f, 0.f, 44.f, 44.f);
        self.imageView.frame  = CGRectMake(0.f, 0.f, 44.f, 44.f);
    
        CGRect rect           = self.textLabel.frame;
        rect                  = 44.f + 6.f;
        self.textLabel.frame  = rect;
    
        rect                  = self.detailTextLabel.frame;
        rect                  = 44.f + 6.f;
        self.detailTextLabel.frame = rect;    
    }
    
  3. 如何自定义UITableView的Section的title的字体和颜色?

    很多时候,我们并不想要系统section的title的字体和颜色,而是希望能够自定义title的字体和颜色,以便适配我们自己的app。具体的修改方法,实现代理UITableViewDataSource中 tableView:titleForHeaderInSection: 修改title内容;tableView:viewForHeaderInSection: 自定义title。

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  
        if(section == 0) {
            return "可能要分享的人";
        } else {
            return nil;
        }
    }  
    
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
    {  
        NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];  
        if (sectionTitle == nil) {  
            return nil;  
        }  
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.width, 24.0f)];  
        label.backgroundColor = [UIColor whiteColor];  
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:12.f];  
        label.text = sectionTitle;  
    
        return label;  
    }
    

    备注:footerView也可类似设置。

  4. 如何去除UITableView中多余的分割线?

    当我们的TableView显示内容不够一屏的时候,默认情况下,屏幕下面多余的空白Cell都会有分割线存在,而我们并不想要这些分割线,那该怎么办呢?其实,很简单,我们只需要对每个空白cell设置一个没有高度的footer view即可。因为对于TableView而言,它会认为有个footer view 要显示,就不会再显示额外的信息。
    可在viewDidLoad方法中设置:

    self.tableView.tableFooterView = [[UIView alloc] init];
    

    也可以在代理 UITableViewDataSource 的 cellForRowAtIndexPath:方法中添加根据具体情况,添加以下代码:

    [tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    
  5. 如何修改UITableView的分割线的宽度?

    有时候,系统默认的分割线的宽度和我们预期的并不一样,这时候就需要我们自己来更改分割线的宽度,具体方法可在 UITableViewDelegate的tableView:willDisplayCell:forRowAtIndexPath:中添加如下代码:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        // Remove seperator inset
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:cell.separatorInset];
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
    
        // Explictly set your cell's layout margins for >= ios8
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:cell.separatorInset];
        }
    }
    
  6. 修改UITableView Section索引的背景颜色和字体颜色

    比较简单,直接上代码:

    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    self.tableView.sectionIndexColor = [UIColor colorWithHex:0x666666];
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值