2013-03-22 38 views
0

我試圖將背景圖像設置爲UITableViewCell中的標籤,但在運行時,只有背景圖像顯示在標籤中,文本不顯示。當設置背景圖像時,UILabel不顯示文字

這是我的代碼

- (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]; 


    if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    UIImageView *preparationLabelBg = [[UIImageView alloc] 
            initWithImage:[UIImage imageNamed:@"cellbg.jpeg"]]; 
    [cell.preparationLabel addSubview:preparationLabelBg]; 
    cell.preparationLabel.backgroundColor=[UIColor clearColor]; 
    [cell.preparationLabel sendSubviewToBack:preparationLabelBg]; 
    [cell.preparationLabel setOpaque:NO]; 

} 

[cell.preparationLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]]; 

// 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; 

}

爲什麼不在這裏顯示的文本?誰能幫助?

回答

4

試試這個代碼,

theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellbg.jpeg"]]; 
0

不要添加背景圖像UILable而不是TableCell的添加背景圖片subnview或添加背景圖片[cell.contentview addSubview:[UIImage imageNamed:@"cellbg.jpeg"]]

還可以使用PNG而不是JPEG/JPG。蘋果更喜歡PNG。

0

而不是把背景的lable放一個ImageViewlable背面,然後將圖像設置爲ImageView。它會工作。