UITableView使用总结

一:tableView知识点

//设置分割线的样式为无
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
          
//设置分割线的颜色
self.tableView.separatorColor = [UIColor redColor];
     
//设置分割线的上下左右,受影响的只有左右,
self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
     
//设置内边距
self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
     
//有内容的有下划线,没内容的无下划线
self.tableView.tableFooterView = [[UIView alloc]init];

// 当选中某行的时候触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     //立刻让当前行变为非选中
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//设定tableView具体某一行的高度,或者整体的每一行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     if (indexPath.row % 2 == 0) {
         return 100;
     }else{
         return 55;
     }
}

//iOS8之后的自适应高度,(cell的垂直布局上下要记得设置)
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
     

二:cell的总结

//cell的frame从新布局  设置cell的尺寸,一般用来设置cell之间的分割线
 -(void)setFrame:(CGRect)frame{
     CGFloat margin = 10;
     frame.origin.x = margin;
     frame.size.width -= 2 * margin;
     frame.size.height -= margin;
     frame.origin.y += margin;
     [super setFrame:frame];
}

//初始化控件创建
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
      //创建控件
      self.selectionStyle = UITableViewCellSelectionStyleNone;
      self.contentView.backgroundColor = [UIColor whiteColor];
      self.backgroundColor = [UIColor whiteColor];
      self.layer.shouldRasterize = YES;
      self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    }
    return self;
}

//添加系统箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//设置cell的颜色
self.contentView.backgroundColor =  [UIColor redColor];

//让用户点击cell不变色
 cell.selectionStyle =UITableViewCellSelectionStyleNone;
     
//设置每个cell的背景图片
cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_apple_small"]];
     
//设置点击cell时的颜色
UIView *clickView = [[UIView alloc]init];
clickView.backgroundColor = [UIColor colorWithRed:191.0/255.0 green:219.0/255.0 blue:229.0/255.0 alpha:0.3];
cell.selectedBackgroundView = clickView;

三:tableView的简单使用

3-1:声明重用标示符

//重用标示符
static NSString * const CellID = @"CellID";

3-2:注册cell
此处不考虑xib,storyboard,作者风格偏纯代码

//注册cell
[self.tableView registerClass:[WYCell class] forCellReuseIdentifier:CellID];

3-3:使用

//有多少个section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//section里有多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.topics.count;
}
//赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RBCheckRegistrationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    cell.invitationActivityModel = self.topics[indexPath.row];
    return cell;
}

四:UITaleView的Header和Footer使用,

4-0:为每个section的Header或者Footer添加标题,实现以下2个方法即可,不再需要其他的操作。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

4-1:如果你不满意仅有标题的Header和Footer,可以为Header和Footer自定义View。
自定义Header的View,实现UITableViewDelegate的:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = WYRGBColor(214, 87, 76);

    //时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = @"时间";
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = DEF_SFont15;
    timeLabel.textColor = [UIColor whiteColor];
    [timeLabel sizeToFit];

    //添加控件
    [headView addSubview:timeLabel];

   //随意设置下位置
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView);
        make.left.equalTo(headView.mas_left).offset(padding);
    }];
    return headerView;
}

除了实现UITableViewDelegate的方法返回自定义View之外,必须实现UITableViewDelegate的方法,明确返回Header的高,才能使自定义的View显示出来。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

4-2:自定义Footer的View,和上面实现的header一样,方法如下

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView ...
    return footerView;
}

明确返回Footer的高,才能使自定义的View显示

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
5:删除cell
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从数据源中删除
    [self.dataArray removeObjectAtIndex:indexPath.row];
    // 从列表中删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

五:tableView头部视图拉伸放大

头部拉伸.gif

1、三个不可缺少的属性

//头部View
@property(nonatomic,strong)UIView *headView;
//头部图片
@property(nonatomic,strong)UIImageView *headImageView;
//状态栏注意事项
@property(nonatomic,assign)UIStatusBarStyle statStyle;
//图片高度   
#define kHeadViewHeight 200

2、实现的关键方法

//创建tableView
-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
    //设置tableView间距
    self.tableView.contentInset = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
    [self.view addSubview:self.tableView];
}

-(void)initHeadView{
    //状态栏初始化
    _statStyle = UIStatusBarStyleLightContent;
    //头部视图
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYScreenW, kHeadViewHeight)];
    self.headView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.headView];
    //头部图片
    self.headImageView = [[UIImageView alloc]initWithFrame:self.headView.bounds];
    self.headImageView.image = [UIImage imageNamed:@"headimage.jpg"];
    self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.headImageView.clipsToBounds = YES;//图片过大的裁切
    [self.headView addSubview:self.headImageView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsety = scrollView.contentOffset.y+scrollView.contentInset.top;
    NSLog(@"%f",offsety);
    if (offsety <= 0)
    {
        //放大
        CGRect frame= self.headView.frame;
        frame.origin.y = 0;
        
        frame.size.height = kHeadViewHeight - offsety;
        self.headView.frame = frame;
        
        frame = self.headImageView.frame;
        frame.size.height = self.headView.frame.size.height;
        self.headImageView.frame = frame;

        //导航栏的设置
        //设置透明度
        self.headImageView.alpha = 1.0;
        //根据透明度修改状态栏颜色
        _statStyle = UIStatusBarStyleLightContent;
        //主动更新状态栏
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
        
    }else{
        //整体移动
        CGRect frame= self.headView.frame;
        //header的最小y值
//        CGFloat min = kHeadViewHeight - 64;
        CGFloat min = kHeadViewHeight;
        frame.origin.y = -MIN(min, offsety);
        
        frame.size.height = kHeadViewHeight;
        self.headView.frame = frame;
        
        frame = self.headImageView.frame;
        frame.size.height = self.headView.frame.size.height;
        self.headImageView.frame = frame;

        //导航栏的设置
        CGFloat progress = 1-(offsety/min);
        self.headImageView.alpha = progress;
        //根据透明度修改状态栏颜色
        _statStyle = (progress < 0.5) ? UIStatusBarStyleDefault:UIStatusBarStyleLightContent;
        //主动更新状态栏
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
    }
}

//状态栏方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

导航栏

勤学如早春之苗,不见其增,日有所涨。
辍学如磨刀之石,不见其减,日有所损。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容