2012-08-13 70 views
0

我想更改分組表格視圖單元格的背景。假設你有5個分組單元格。頂部單元格是左上角和右下角的圓角,底部單元格是左下角和右下角的圓角,中間單元格沒有圓角。這是問題所在。如果我設置每個單元格的背景,頂部和底部單元格將看起來就像中間沒有頂部和底部角落的單元格。我如何設置背景,使頂部和底部的單元格變圓而不會導致每個單元格看起來相同。提前致謝。如何更改分組的tableview單元格的背景?

編輯: 另一方面說明。有些情況下,tableview單元格只顯示一個單元格,這意味着每個角落都是圓角的。那麼如何設置可以處理所有這些情況的背景。

+0

要變更背景視圖或顏色? – Neo 2012-08-13 05:57:55

+0

不是我想象的顏色。因爲我試圖添加漸變背景。 – tipsywacky 2012-08-13 06:06:57

+1

嘗試cell.backgroundView.clipsToBounds = YES; – 2012-08-13 07:03:00

回答

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

    } 

} 
相關問題