2013-03-21 5 views
3

UITableViewCell에 배경 이미지를 추가하려고합니다. 받는 사람 변경 얻을 것이다 응용 프로그램을 실행하고있는 tableview를 선택하는 동안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; 
} 

내 문제가있다, 먼저 기본 흰색 배경 및 시간 셀 배경 후 테이블 셀을 보여줍니다 이 내가 사용하고있는 코드입니다 욕망 이미지 ... 왜 내가 원하는 셀 배경으로 테이블 뷰를로드하는 데 오랜 시간이 걸리는지 알 수 없다.

+0

참조 http://stackoverflow.com/a/15353155/1713478 그것은 당신을 도울 것입니다 답변 – Pratik

답변

5

느린 로딩은 생성 할 때만이 아니라 언제든지 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] ]; 
    } 
3

당신은 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] ]; 

라인을 이동해야합니다. 그렇지 않으면 셀은 재활용 된 후에 만 ​​새로운 배경을 얻게되며 새로 만든 배경은 배경없이 남아있게됩니다.

2
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]; 
} 
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; 


}