2011-06-10 138 views
0

这个问题很简单,但很烦人。我在视图控制器中放置了一个分组样式的UITableView,并使控制器成为表视图的委托和数据源。自定义UITableView背景颜色

当我什么改变表格的背景颜色,我用:

UIColor *backColor = [UIColor colorWithRed:(15.0/255.0) green:(170.0/255.0) blue:(230.0/255.0) alpha: 0.75f]; 

[self.myTable setBackgroundColor:backColor]; 

tableView:tableView cellForRowAtIndexPath:indexPath方法,我只返回一个简单的细胞:

static NSString *CellIdentifier = @"CellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil){ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:moreCellIdentifier] autorelease]; 
} 

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
return cell; 

但奇怪事情是在每个单元格的圆角处颜色是不正确的,我认为这是因为单元格的其中一个子视图的框架大于单元格,我试图使子视图的框架很小但仍然失败任何想法?谢谢。

+0

我认为这是单元格宽度的问题,我将UITableViewCell分类并使宽度仅为200的新单元类,但在表格视图中单元格仍占用几乎整个屏幕宽度。 – joe522 2011-06-10 17:40:53

回答

0

请尝试在您的tableView:tableView cellForRowAtIndexPath:indexPath函数中使用下面的代码。

NSString *CellIdentifier = [[NSString alloc] initWithFormat:@"CellIdentifier_%d_%d",indexPath.section,indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil){ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
    reuseIdentifier:moreCellIdentifier] autorelease]; 
    } 
    [CellIdentifier release]; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    return cell; 
+0

这些代码只是改变了为细胞分配细胞标识符的方式,它与我在这里遇到的问题无关。 – joe522 2011-06-10 17:15:50

+0

@ joe522:你试过了吗? – Jhaliya 2011-06-10 17:17:10

+0

@Jhaliya:是的,它不起作用。我发现这是单元格宽度的问题,我将UITableViewCell分类并使宽度仅为200的新单元类,但在表格视图中,单元仍占用几乎整个屏幕宽度。 – joe522 2011-06-10 17:40:06

0

看你的代码,这是我不明白这行:

[self.myTable setBackgroundColor:backColor]; 

指。什么是myTable?什么是自我?如果你继承UITableViewController,最好的地方在哪里执行的代码是viewWillAppear

UIColor *backColor = [UIColor colorWithRed:(15.0/255.0) green:(170.0/255.0) blue:(230.0/255.0) alpha: 0.75f]; 
tableViewController.tableView.backgroundColor = backColor; 

(使用:

无论如何,如果你使用的是UITableViewController来管理你的表,你应该改变背景颜色这样的自己而不是tableViewController.tableView.backgroundColor)。