UITableView-Style的使用

UITableView的样式
UITableView的样式有两种,一种为UITableViewStylePlain普通样式(默认样式);一种为UITableViewStyleGrouped分组样式。

typedef NS_ENUM(NSInteger, UITableViewStyle) {
    UITableViewStylePlain,          // regular table view 常规
    UITableViewStyleGrouped         // preferences style table view 偏好样式
};

创建UITableView
创建UITableView的方法有三种,如果不指定tableView的样式,则默认使用UITableViewStylePlain样式。

 //1.创建对象,不指定tableView的样式
UITableView * tableView = [UITableView new];
UITableView * tableView = [[UITableView alloc]init];
//2.创建对象,并设置其frame,不指定tableView的样式
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
//3.创建对象,并设置其frame,指定tableView的样式为UITableViewStylePlain
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

同样的,当使用UItableViewController时,如果不指定tableView的样式,则默认使用UITableViewStylePlain样式。

1.创建视图控制器,不指定tableView的样式
YXSecTableViewController * c = [YXSecTableViewController new];
YXSecTableViewController * c = [[YXSecTableViewController alloc]init];
2.创建视图控制器,指定tableView的样式为UITableViewStyleGrouped
YXSecTableViewController * c = [[YXSecTableViewController alloc]initWithStyle:UITableViewStyleGrouped];

例子
这是本次例子使用的设计图:

设计图

根据这个设计图, 我们可以分析,该tableView的样式应该为UITableViewStyleGrouped,没有tableHeaderViewsectionHeaderViewtableFooterView,没有分割线,但有一个高度为5的sectioFooterView,用作分割各个单元格。
先不要吐槽,我知道,在cell中创建一个高度为5的view就可以了,后面会讲到。

分析完成
创建YXFirstTableViewController,继承UITableViewController,tableView的样式为默认样式。设置cell的高度为自动计算。使用xib来自定义cell,设置storeArr.countsection,每个section只有1个cell。使用属性设置sectionFooterHeight为5。

====== AppDelegate ======
- (UIViewController *)setRootVC{
    UIViewController * c = [NSClassFromString(@"YXFirstTableViewController") new];
    UINavigationController * navi = [[UINavigationController alloc]initWithRootViewController:c];
    return navi;
}

====== YXFirstTableViewController ======
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define YXRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255. blue:b/255.0f alpha:1]
#define YXSameRGB(v) YXRGB(v,v,v)

#pragma mark ============== Init ==============
- (void)setUpTable{
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.sectionFooterHeight = 5;
    [self.tableView registerNib:[UINib nibWithNibName:@"YXStoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"YXStoreTableViewCell"];
}

#pragma mark ============== TableView ==============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return storeArr.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view= [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 5)];
    view.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:220/255.0 alpha:1];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YXStoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YXStoreTableViewCell"];
    cell.model = storeArr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s",__func__);
}

运行:使用属性设置self.tableView.sectionFooterHeight = 5;没有起作用。

并且当我的section数量只有几个时(显示的内容小于tableView的高度时),tableview最底端还会出现‘格外’的分割线。
思考:使用属性设置不行,那使用代理方法呢?
先不管分割线问题,我使用代理方法设置sectionFooterHeight的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

运行:设置成功,并且没有了分割线!

但我发现sectionFooter的背景色太浅,使用数码测色计测出颜色为YXSameRGB(247)
使用代理方法设置sectionFooter的背景颜色。

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view= [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 5)];
    view.backgroundColor = YXSameRGB(229);
    return view;
}

运行:成功设置sectionFooter的背景颜色

滑动tableView,仔细观察,发现最底下的sectionFooter竟然跟随tableView一起滚动。
估计如果你也设置了header的话,效果也是一样。
这个效果,有好有坏,但这里,看起来并不友好,并且如果我的sectionFooter高度为30时,还会遮挡cell的内容,本来iphone屏幕就不大,这简直不可原谅。

解决方法:
设置tableViewstyleGroup

当使用UITableViewController时,设置tableViewstyle有两种方法:
由于我习惯使用NSClassFormString()方法创建视图控制器,所以我使用第二种方式设置tableViewstyle

1.创建视图控制的时候设置其style
YXSecTableViewController * c = [[YXSecTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
2.覆盖其初始化方法,指定你想要的样式
-(instancetype)initWithStyle:(UITableViewStyle)style{
    if (self = [super initWithStyle:UITableViewStyleGrouped]){
    }
    return self;
}

运行:成功解决sectionFooter跟随tableView一起滚动的效果

但是出现了另外两个问题:
1.tableView的背景色改变了,使用数码测色计测出颜色为YXRGB(239,239,244),原来是白色
2.多了tableHeaderView
3.多了sectionHeaderView

1.删除sectionHeaderView
有鉴于前面使用属性设置sectionFooter的高度无效的情况,这次我使用代理方法设置sectionHeaderHeight

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

运行:代码不起作用

思考,使用代理方法设置sectionHeaderView能起作用吗?

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view= [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 0)];
    return view;
}

运行:这仍然不起作用
一番折腾后,我发现:

1.当headerView的高度为0时,tableview会使用默认高度来设置sectionHeaderView的height
2.-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
代理方法的使用前提是要设置sectionHeaderHeight,否则该方法不起作用。

先不管footer是否也一样呢?
反推过来,当我的tableViewstylePlian时,使用属性设置sectionFooterHeight不起作用,需要使用代理方法来设置。
那么当我的tableViewstyleGroup时,我使用代理方法来设置sectionHeaderHeight不起作用,那使用属性设置呢?
使用属性设置sectionHeaderHeight

self.tableView.sectionHeaderHeight = 0;

运行:成功设置了sectionHeaderHeight

2.删除tableHeaderView
tableViewstlyePlain时,我们想删除tableView底部格外的分割线有两种方法:

1.设置tableFooterView为一个size为0的view
self.tableView.tableFooterView = [UIView new];
2.设置分割线的样式为无分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

使用同样技巧,我设置tableHeaderView为一个size为0的view,虽然当前当styleGroup,但我认为也是一样的

self.tableView.tableHeaderView = [UIView new];

运行:遗憾的是,这并不起作用。

我尝试使用以下方法,虽然我觉得结果是一样的。

[self.tableView setTableHeaderView:[UIView new]];

运行:果然还是不行。

没有办法了,你们呢?我只好把tableHeaderView的高度设置为5,这跟sectionFooterView一样了,这样看起来效果会好点。

UIView * tableHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 5)];
tableHeader.backgroundColor = YXSameRGB(229);
self.tableView.tableHeaderView = tableHeader;

好吧,看来这样的做法并不能达到跟设计图一样的效果。

在cell中创建一个高度为5的view
其实最简单的方法是在cell中创建一个高度为5的view,
创建YXSecTableViewController继承自UITableViewControllertableView的样式为默认样式Plain,设置分割线的样式为无分割线。

#pragma mark ============== Init ==============
- (void)setUpTable{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    [self.tableView registerNib:[UINib nibWithNibName:@"YXSecStoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"YXSecStoreTableViewCell"];
}

#pragma mark ============== TableView ==============
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return storeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YXSecStoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YXSecStoreTableViewCell"];
    cell.model = storeArr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s",__func__);
}

运行:没有问题。

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

推荐阅读更多精彩内容

  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 3,268评论 0 15
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 895评论 0 1
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,811评论 2 10
  • 所有人和事, 自己问心无愧就好, 不是你的也别强求, 反正离去的, 都是风景, 留下的, 才是人生。 ​ ​​​ ...
    笃学青衿阅读 200评论 0 1
  • 2014年初开始规划小百相机,由于事情太多导致版本迭代很慢,全年才发布三个版本,用户也才17000+,月收入$10...
    hi_liyipeng阅读 357评论 0 2