如何更改所有UITableViewCells的backgroundColor
我有一个非常简单的视图控制器只有一个UITableView
和一个UIButton
,当按下button,我想改变所有UITableViewCells
的背景颜色为绿色,给出有一些单元格不可见,我用这个循环完成我所需要的:
- (IBAction)click:(id)sender { for (int row = 0; row < [self.tableView numberOfRowsInSection:0]; row++) { NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:0]; UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath]; cell.backgroundColor = [UIColor greenColor]; } }
问题是UITableView
的默认行为,它实际上并没有创build不可见的单元格,直到它们可见! 所以上面的代码不幸的只适用于可见的单元格。 问题是,如何更改button水龙头上的所有单元格的颜色?
ps这个非常简单的项目样例可以在这里下载 。
先谢谢你。
你不必这样做
只需使用一个variables来保存单元格的backgroundColor
UIColor cellColor;
那么当你改变颜色调用
[tableView reloadData];
然后进入
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = yourcell; ... ... ... cell.backgroundColor = cellColor; return cell ; }
完成!
更新
对不起,试试这个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = yourcell; ... ... ... cell.textLabel.backgroundColor = [UIColor clearColor]; UIView* bgview = [[UIView alloc] initWithFrame:cell.bounds]; bgview.backgroundColor = [UIColor greenColor]; [cell setBackgroundView:bgview]; return cell ; }
你想使用UITableViewDataSource来处理这个,所以只需在你的.h文件中创build一个UIColorvariables,在你的动作中改变这个variables,然后告诉tableView重新载入它的数据,然后在数据源响应中设置颜色。
MyTableViewController.h
@interface MyTableViewController : UITableViewController { UIColor *cellColor; } -(IBAction)click:(id)sender;
MyTableViewController.m
@implementation MyTableViewController -(IBAction)click:(id)sender{ cellColor = [UIColor redColor]; [self.tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Setup your cell cell.textLabel.backgroundColor = [UIColor clearColor]; cell.detailTextLabel.backgroundColor = [UIColor clearColor]; cell.accessoryView.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = cellColor; return cell; } @end
你在评论保罗·亨特的回答“不染色整个细胞”。
尝试设置一个backgroundView:
cell.backgroundView = [[[UIView alloc] init] autorelease]; cell.backgroundView.backgroundColor = self.cellColor;
将颜色存储为保留property
,并在单击button时进行设置。
@property (retain, nonatomic) UIColor *cellColor; .... - (IBAction)click:(id)sender { self.cellColor = [UIColor greenColor]; for (int row = 0; row < [self.tableView numberOfRowsInSection:0]; row++) { NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:0]; UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath]; cell.contentView.backgroundColor = self.cellColor; cell.textLabel.backgroundColor = self.cellColor; } }
您也可以select重新加载整个tableView
而不是遍历单元格。
- (IBAction)click:(id)sender { self.cellColor = [UIColor greenColor]; [self.tableView reloadData]; }
然后在你的tableView:cellForRowAtIndexPath:
检查property
是否设置,并设置contentView
和textLabel
背景颜色。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... if (self.cellColor) { cell.contentView.backgroundColor = self.cellColor; cell.textLabel.backgroundColor = self.cellColor; } return cell; }
在你的代码中改变这个方法
可能这是你的临时解决scheme
-(IBAction)click:(id)sender { self.tableView.backgroundColor=[UIColor greenColor]; }
这个链接可以解决你的问题,它有类似的问题,如设置iPhone上的表格视图单元格的背景颜色
- 我的应用程序只在iPod上显示黑屏
- 将AVPlayerViewController背景更改为专辑图片objective-c iOS
- 如何使用AFNetworking 2.0通过Rest API注册?
- 无法将types'UITableViewCell'的值转换为'(AppName)。(CustomCellName)'
- 单击单元格中的button时,如何parsing以播放某首歌曲?
- 使用TwitterKit iOS的访客身份validation获取用户时间线时的错误代码403
- 应用程序窗口应在XCode 5中的应用程序启动结束时具有根视图控制器
- 带有加速框架vDSP的iPhone FFT
- 适用于iOS的日历日和周视图