【发布时间】:2011-04-04 02:42:49
【问题描述】:
我有一个表格视图。在那我想单独设置第一个单元格的颜色..其他应该是白色的...我该怎么做
我需要你的帮助....
提前致谢.....
【问题讨论】:
标签: iphone
我有一个表格视图。在那我想单独设置第一个单元格的颜色..其他应该是白色的...我该怎么做
我需要你的帮助....
提前致谢.....
【问题讨论】:
标签: iphone
在 cellForRowAtIndexPath 中,检查 indexPath.row == 0,然后设置自定义背景。否则设置默认背景。
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if (indexPath.row == 0) {
//set custom background color
} else {
//set default background color
}
return cell;
}
【讨论】:
您应该在“willDisplayCell”委托方法中进行颜色更改,而不是在“cellForRowAtIndexPath”中创建它。 否则,在向单元格中添加配件等时,它可能不会粘住或显示不良。
查看这篇文章了解更多详情:Setting background color of a table view cell on iPhone
【讨论】:
if(indexPath.row == 0)
{
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1];
cell.backgroundView = bg;
[bg release];
}
【讨论】: