没有填充整个单元格的单元格背景视图

我为UITableView使用单独的UITableView ,当我设置此单元格的背景视图时,它似乎将图像填充到一个框中而不是整个单元格中。 单独的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; } 

试试这个代码。 它为我工作。

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

尝试这个?

 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; 

你正在使用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]; } } 

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