开发者

Customize UITableView background color

The problem is quite simple but is pretty annoying. I put a groupe style UITableView in a view controller and make the controller the table view's delegate and datasource.

When I what to change the color of the background of the table, I use:

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];

and in tableView:tableView cellForRowAtIndexPath:indexPath method, I just return a simple cell:

static NSString *CellIdentifier = @"CellIdentifier";

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

cell.accessoryType = UITableViewCellAccessoryDetai开发者_JAVA技巧lDisclosureButton;
return cell;

But the strange thing is that at the round corner of each cell the color is just not right, and I think it's because the frame of one of the subviews of the cell is larger then the cell and I tried to make the frames of subviews small but still failed.Any idea?Thanks.


Try with using the below code in your tableView:tableView cellForRowAtIndexPath:indexPath function.

   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;


Looking at your code, it is not clear to me what this line:

[self.myTable setBackgroundColor:backColor];

refers to. What is myTable? and what is self?

Anyway, if you are using a UITableViewController to manage your table, you should change the background color like this:

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;

If you subclass UITableViewController, the best place where to execute that code is viewWillAppear (using self instead of tableViewController.tableView.backgroundColor).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜