UITableView自定义分割线、改变Header和Footer的背景颜色
来源:互联网 发布:ubuntu虚拟机联网 编辑:程序博客网 时间:2024/05/18 12:03
- 自定义分割线
1、去除系统原生separator:
self.tableView.separatorColor = [UIColor clearColor];
2、在UITableView的协议方法里面自定义分割线:
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// 画一个像素的线 UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 44-(1.0f/[UIScreen mainScreen].scale)/2, cell.width, 1.0f/[UIScreen mainScreen].scale)]; line.backgroundColor = [UIColor lightGrayColor]; [cell.contentView addSubview:line];
正确绘制一个像素的方法:
(1.0f/[UIScreen mainScreen].scale)/2
1.0f/[UIScreen mainScreen].scale
奉上两个宏:
#define SINGLE_LINE_WIDTH (1 / [UIScreen mainScreen].scale)#define SINGLE_LINE_ADJUST_OFFSET ((1 / [UIScreen mainScreen].scale) / 2)
参考:http://www.cocoachina.com/ios/20150629/12278.html
- 改变Header和Footer的背景颜色
问题:
改变UITableView的header、footer背景颜色,这是个很常见的问题。之前知道的一般做法是,通过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色。但是今天发现一个更简洁的做法。
更简洁的方法:
对于iOS 6及以后的系统,改变header、footer的背景颜色,实现这个新的delegate函数即可:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.tintColor = [UIColor clearColor];}
还有改变header、footer的文字颜色:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{ UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; [footer.textLabel setTextColor:[UIColor lightGrayColor]];}
错误的尝试:
写这篇文章的目的,主要是想记录两种错误的尝试。
当看到这个Delegate函数时,第一反应是想当然地这样做:
错误尝试1
- (void)tableView:(UITableView )tableView willDisplayFooterView:(UIView )view forSection:(NSInteger)section {
view.backgroundColor = [UIColor clearColor];
}
这样做是无效的,无论对什么颜色都无效。
错误尝试2
- (void)tableView:(UITableView )tableView willDisplayFooterView:(UIView )view forSection:(NSInteger)section
{
UITableViewHeaderFooterView footer = (UITableViewHeaderFooterView )view;
footer.contentView.backgroundColor = [UIColor redColor];
}
这样做设成不透明的颜色就没问题。但设成clearColor,看到的还是灰色。
若有错误,还望指正,谢谢!
- UITableView自定义分割线、改变Header和Footer的背景颜色
- 关于UITableView的间隔问题(header和footer)和分割线
- 自定义UITableView的Header和Footer
- iOS UITableView分割线颜色,长度自定义
- Android listView 去掉header和footer中的分割线
- 设置UITableView分割线的颜色和位置
- 改变tableview的分割线长、颜色问题 以及自定义分割线
- 自定义UITableView的header颜色
- uitableview指定的分割线的颜色
- UITableView 自定义分割线
- UILabel和UITableview自定义分割线
- UITableView 去掉背景 分割线
- 改变NumberPicker文字,分割线的颜色
- Listview中的分割线、分割线的颜色以及分割线高度的改变 Button或者ImageButton的背景设为透明或者半透明
- iOS小技巧-UITableView去除底部多余的分割线方法以及设置分割线风格和颜色
- 自定义tableview Header和Footer的样式
- UITableView 背景颜色不能改变的原因
- 改变UITableView的headerView、footerView背景颜色
- Java生产消费者模式之项目实践
- urllib
- VS2013 Qt5显示中文字符
- Codeforces 558A Lala Land and Apple Trees(水题)
- 图的存储
- UITableView自定义分割线、改变Header和Footer的背景颜色
- Leetcode 236 Lowest Common Ancestor of a Binary Tree
- Android中visibility三个属性的区别
- 在C#中实现Word页眉页脚的所有功能
- ASP.Net MVC从客户端中检测到有潜在危险的 Request.Form 值
- 6天敏捷 企业邮件管理系统(YII框架)
- LeetCode224:Basic Calculator
- iOS 字符串截取,将字符串中用括号包括的内容去除
- 把26个字母分成3组,有多少种分法