UITableViewCell背景图像设置
问题描述:
我尝试将背景图像添加到我的UITableViewCell
。 这是我使用的代码:UITableViewCell背景图像设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableCell";
// Dequeue or create a cell of the appropriate type.
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell.preparationLabel setFont:[UIFont fontWithName:@"Bold" size:20]];
// Configure the cell.
if (btnselected==1)
{
cell.preparationLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
}
else
{
cell.preparationLabel.text = [recipe.preparation objectAtIndex:indexPath.row];
}
return cell;
}
我的问题是,当运行应用程序并选择实现代码如下,首先显示了默认的白色背景和一定的时间单元格背景后的表格单元格将得到改变的渴望图像......我不知道为什么它需要很长时间来加载表格视图与我想要的单元格背景
答
加载缓慢是因为您每次都将UIImageView
添加到单元格,而不是仅在创建新的一个。
如下更改代码:
// Dequeue or create a cell of the appropriate type.
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
}
答
您需要if (cell == nil)...
条件后移动
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
线。否则,细胞只有在回收后才会获得新的背景,而新生成的背景则没有背景。
答
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
UIImageView *imgView1 = [[UIImageView alloc]init];..continue...
UIImageView *imgView2 = [[UIImageView alloc]init];..continue...
[cell.backgroundView addSubView:imgView1];
[cell.selectedBackgroundView addSubView:imgView2];
cell = [nib objectAtIndex:0];
}
答
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *ivwCellHighlighted = [[UIImageView alloc]initWithImage:[UIImage imageNamed:CELL_SELECTEDIMAGE]];
[ivwCellHighlighted setFrame:CGRectMake(1, 1, 320, 47)];
[cell setSelectedBackgroundView:ivwCellHighlighted];
[ivwCellHighlighted release];
ivwCellHighlighted=nil;
}
参考http://*.com/a/15353155/1713478回答它会帮助你 – Pratik 2013-03-21 10:38:27