1

我使用单独UITableViewCellUITableView,当我设置此单元格的背景视图时,它似乎将图像填充在一个框中而不是整个单元格中。单独UITableViewCellUITableView's行的尺寸都是280x44。这是它的样子: 在此处输入图像描述

这是我为设置单元格的背景视图而编写的代码:

- (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;
}
4

3 回答 3

2

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

UIImageView * ac= [[UIImageView alloc] init];
ac.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
cell.backgroundView =ac;
cell.backgroundColor = [UIColor clearColor];
于 2013-03-12T04:17:11.343 回答
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;
于 2013-03-12T04:01:01.893 回答
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];
    }
}

上面的代码你的问题将解决。

于 2013-03-12T04:10:55.723 回答