0

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

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

4

2 回答 2

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

    }

}
于 2012-08-13T06:44:28.063 回答
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;
   }
于 2012-08-13T06:50:52.520 回答