细胞背景视图不填充整个细胞

时间:2013-03-12 03:55:09

标签: ios uitableview

我为UITableViewCell使用单独的UITableView,当我设置此单元格的背景视图时,它似乎将图像填充在一个框中而不是整个单元格中。单独的UITableViewCellUITableView's行的维度均为280x44。这是它的外观图像: enter image description here

这是我正在编写的用于设置单元格背景视图的代码:

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

    PBDashboardSummarizedCell *cell = (PBDashboardSummarizedCell *)[tableView dequeueReusableCellWithIdentifier:@"PBDashboardSummarizedCell"];
    if (!cell) {
        cell = [PBDashboardSummarizedCell standardCell];
    }
     if(datapickup.isexpanded==YES)
          {
           UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
              av.opaque = NO;
              av.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dashboardCellEffect.png"]];
              cell.backgroundView = av;          //tried this
             // [cell setBackgroundView:av];     //and this also... but still image is set in the box and not the whole cell
          }
          else{
              UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
              av.backgroundColor = [UIColor clearColor];
              av.opaque = NO;
              cell.backgroundView = av;

        }

          return cell;
}

3 个答案:

答案 0 :(得分:2)

试试这段代码。它对我有用。

UIImageView * ac= [[UIImageView alloc] init];
ac.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
cell.backgroundView =ac;
cell.backgroundColor = [UIColor clearColor];

答案 1 :(得分:1)

试试这个吗?

UIImage *backgroundImage = [UIImage imageNamed:@"dashboardCellEffect.png"];
UIView *backgroundCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
// redraw the image to fit cell's size
UIGraphicsBeginImageContextWithOptions(backgroundCellView.frame.size, NO, 0.f);
[backgroundImage drawInRect:CGRectMake(0.f, 0.f, backgroundCellView.frame.size.width, backgroundCellView.frame.size.height)];
UIImage *refinedImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();

[backgroundCellView setBackgroundColor:[UIColor colorWithPatternImage:refinedImage]];
cell.backgroundView = backgroundCellView;

答案 2 :(得分:1)

您正在使用imageview,因此它仅在imageview而不是整个单元格中显示颜色

您可以尝试以下方法

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(datapickup.isexpanded==YES)
    {
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dashboardCellEffect.png"]];
    }
    else {
         cell.backgroundColor = [UIColor clearColor];
    }
}

以上代码,您的问题将解决。