在tableView中定义 cell的背景颜色

2011-06-22

给tableview中的 cell 添加 背景颜色 ![null](http://jiaxin.im/wp- content/uploads/2011/06/屏幕快照-2011-06-22-下午02.10.02.png)

示例代码

- (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...

    [[cell textLabel] setText:[Mydata objectAtIndex:indexPath.row]];
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    
//  添加背景颜色
    UIView * backgrdView = [[UIView alloc] initWithFrame:cell.frame];
    backgrdView.backgroundColor = [UIColor redColor];
    cell.backgroundView = backgrdView;
    [backgrdView release];
    return cell;
}