http://haoxiang.org/2010/12/uitableviewcell-background/
UITableViewCell是一个很常用的View,通常我们都是直接使用它。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
return cell;
}
得到这个效果:
现在我们给tableViewCell加上点背景色:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
// cell.backgroundColor = [UIColor blueColor];
cell.contentView.backgroundColor = [UIColor blueColor];
return cell;
}
我们不应该直接使用cell.backgroundColor。Cell本身是一个UIView,我们所看到的部分其实只是它的一个Subview,也就是cell.contentView。所以,如果直接改变cell本身的背景色,依然会被cell.contentView给覆盖,没有效果。
不过,通过cell.contentView.backgroundColor来改变背景色还不是最好的Practice. 如果通过
进入Edit模式,就会出现问题。
Cocoa提供的按钮背景色为透明。因为ContentView被移开,下面是tableView的颜色,已经不是cell的一部分了。
所以,最好的方式应该是通过cell.backgroundView来改变cell的背景。按照文档说明,backgroundView始终处于cell的最下层,所以,将cell里的其它subview背景设为[UIColor clearColor],以cell.backgroundView作为统一的背景,应该是最好的方式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgrdView = [[UIView alloc] initWithFrame:cell.frame];
backgrdView.backgroundColor = [UIColor blueColor];
cell.backgroundView = backgrdView;
[backgrdView release];
return cell;
}
效果:
分享到:
相关推荐
自定义UITableViewCell背景的切换
在不使用现用图片来实现UITableView中单元格的渐变背景 利用Quartz2D绘图绘制渐变背景
本文主要介绍的是关于UITableViewCell在编辑状态下背景颜色的修改方法,分享出来供大家参考学习,下面来一起看看详细的介绍: 一、先看下效果图 二、网上很多下面这种答案 UITableViewCell * cell = [tableView ...
使用不同的文本颜色和背景颜色自定义每个按钮。 ![加载图片](滑动查看更多/demo.gif) 安装 只需将 GSSwipeableCell 文件夹拖到您的项目中即可。 创建自定义表格单元格子类 GSSwipeableCell 而不是 UITableViewCell...
背景 查看有关SwipeCellKit成为如何的。 演示版 过渡风格 过渡样式描述了在滑动过程中如何显示操作按钮。 边界 拖 揭示 定制 扩展样式 扩展样式描述了当单元格滑过定义的阈值时的行为。 没有任何 选拔 破
SwipeCellKit 基于股票Mail.app的可滑动UITableViewCell / UICollectionViewCell,在Swift中实现。 关于 可滑动的UITableViewCell或UICollectionView...背景 查看我关于SwipeCellKit成为如何的。 演示版 过渡风格 过渡
一行代码设置 UITableViewCell 与导航条间距 19 修改 UITableview 滚动条颜色的方法 19 下文件之前获取到文件大小的代码 19 网络编程总结 iphone 19 Iphone 实现画折线图 25 让iPhone屏幕常亮不变暗的方法 27 苹果...
自定义UITableViewCell。实现在列表上加入各种徽章(badgeString)效果。比如加上有背景颜色的数字等等。 注意:请在Mac下解压使用
PHF视差背景细胞 UITableView和UITableViewCell扩展,可以避免在用户滚动时出现视差效果。 和Swift真的很容易实现和使用。 入门 将用于远程映像的Alamofire框架添加到您的项目,为 将PHFParallaxBackgroundCell和...
WBSwipeableCell SwipeableMenu对于在Swift中实现的UITableViewCell和UICollectionViewCell都适用。关于可滑动菜单具有以下提到的功能: 左右滑动动作菜单操作按钮,带有:仅图像* 菜单样式:水平,正方形和垂直从左...
背景在 iOS 开发中,UIStoryboards 使用起来很痛苦, 。 两个很大的原因是: 您只能使用字符串文字作为 Storyboard 中视图控制器和 segue 的标识符。 在引用 Storyboard 中的任何内容时,您的源代码中再次需要这些...
来源:Licence:未知平台:iOS设备:iPhone / iPad... 自定义UITableViewCell。实现在列表上加入各种徽章(badgeString)效果。比如加上有背景颜色的数字等等。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。
8.5.3 使用UITableViewCell的自定义子类 159 8.6 分组分区和索引分区 163 8.6.1 构建视图 163 8.6.2 导入数据 164 8.6.3 实现控制器 164 8.6.4 添加索引 168 8.7 实现搜索栏 168 8.7.1 重新考虑设计 168 8.7.2 深层...