如何更改分组的tableview单元格的背景?

时间:2012-08-13 03:43:30

标签: objective-c ios uitableview

我想更改分组表格视图单元格的背景。假设您有5个分组的单元格。顶部单元格在左上角和右上角是圆角,底部单元格在左下角和右下角是圆角,中间单元格没有圆角。这就是问题所在。如果我设置每个单元格的背景,顶部和底部单元格看起来就像中间没有顶角和底角的单元格。如何设置背景,使顶部和底部单元格呈圆形,而不会让每个单元格看起来都相同。提前谢谢。

编辑: 还有另一个注意事项。有些情况下tableview单元格只显示一个单元格,这意味着每个角都是圆角的。那么如何设置可以处理所有这些情况的背景。

2 个答案:

答案 0 :(得分:1)

你试过吗

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:CellIdentifier];


    if (indexPath.row == 0) 
    { 
        NSLog(@"Here my first cell");
        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myFirstCellPicture.png"]];
     } else {
        NSLog(@"myOtherCells");
        cell.contentView.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"otherCellPicture.PNG"]];

    }

}

答案 1 :(得分:1)

您可以为单元格设置背景渐变,在这种情况下,您可能不必担心圆角,因为您使用了cell.layer。 (对于您项目中的导入Quartz框架)

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

            if (indexPath.row == 0)  // first cell
            {
         // set background gradient
         CAGradientLayer *gradient =  [[CAGradientLayer alloc] init];
         gradient.position = CGPointMake(0, 0);
         gradient.frame = cell.frame; 
         gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.207 green:0.207 blue:0.207 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:0.125 green:0.125 blue:0.125 alpha:1.0] CGColor], nil];
             [[cell.layer.sublayers objectAtIndex:0] removeFromSuperlayer];
         [cell.layer insertSublayer:gradient atIndex:0];
         [gradient release];
            }
            else if (indexPath.row == 1) // second cell
            {
            //
            // Another gradient
            //
            }
      }
      return cell;
   }
相关问题