欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

改变iOS应用中UITableView的背景颜色与背景图片的方法

程序员文章站 2023-12-04 21:22:52
改变uitableview的header、footer背景颜色 改变uitableview的header、footer背景颜色,这是个很常见的问题。之前知道的一般做法是,...

改变uitableview的header、footer背景颜色

改变uitableview的header、footer背景颜色,这是个很常见的问题。之前知道的一般做法是,通过实现tableview: viewforheaderinsection:返回一个自定义的view,里面什么都不填,只设背景颜色。但是今天发现一个更简洁的做法:

对于ios 6及以后的系统,实现这个新的delegate函数即可:

复制代码 代码如下:

- (void)tableview:(uitableview *)tableview willdisplayfooterview:(uiview *)view forsection:(nsinteger)section {
 view.tintcolor = [uicolor clearcolor];
}

还可以改变文字的颜色:
复制代码 代码如下:

- (void)tableview:(uitableview *)tableview willdisplayfooterview:(uiview *)view forsection:(nsinteger)section
{
 uitableviewheaderfooterview *footer = (uitableviewheaderfooterview *)view;
 [footer.textlabel settextcolor:[uicolor whitecolor]];
}

修改tableview的背景图片

修改uitableview的背景图片

1.图片显示为'patternimage'模式。

复制代码 代码如下:

// viewdidload

self.tableview.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"backgroundimage"]];

// cellforrowatindexpath

cell.backgroundcolor = [uicolor clearcolor];


这种情况下背景图片像地板砖一样平铺。拉动tableview背景图片会随着动,若行数超过背景图片的高度,会接着显示下一张图片。

2.正常的背景图片。

复制代码 代码如下:

// viewdidload

self.tableview.backgroundcolor= [uicolor clearcolor];

uiimageview*imageview = [[uiimageview alloc]initwithimage:[uiimageimage named:@"backgroundimage"]];

self.tableview.backgroundview = imageview;

// cellforrowatindexpath

cell.backgroundcolor = [uicolor clearcolor];


这种情况下背景图片不会动,即无论多少行看到的都是同样的背景。