2011-09-07 2 views
1

그룹화 된 테이블보기에서 iPhone 응용 프로그램에 대한 UITableViewCell을 아래에서 사용자 지정하려고합니다. 내가 뭘하고 싶은 건 이미지 너비가 패딩 (280)을 빼고 전체 셀을 차지하고 이미지의 크기에 따라 높이 변수를 갖게하는 것입니다.사용자 지정 UITableViewCell 크기 조정 및 이미지 크기를 기반으로 UIImageView 포함

현재 원격 이미지를 비동기 적으로 다운로드하기 위해 SDWebImage를 사용하고 있습니다. 이 경우 올바른 방법이 아닐 수도 있습니다. 또한 사용자 정의 셀에 초기화시 이미지를 제공하는 방법을 찾는 데 문제가 있습니다. 이미지 URL은 DetailViewController의 self.beerPhoto에 저장됩니다.

나는 이것을 여러 가지 방법으로 찾고 있으며 내가 찾고있는 것을 찾지 못했습니다. 가장 가까운 것은 How to scale a UIImageView proportionally 이었지만,이 코드는 작동하지만 초기화 후에 이미지를 설정하면 빈 셀이 남았 기 때문에이 메서드는 셀을 초기화 할 때 이미지가 있어야합니다.

현재 코드에는 이미지를 세로 방향으로 근사하도록 설정된 상수가 포함되어 있습니다. 실제로 이미지의 일부는 세로이고 일부는 가로 방향입니다.

추가 정보가 필요하면 알려주십시오. 맞춤있는 UITableViewCell위한

헤더 : 커스텀있는 UITableViewCell

#import "BeerDetailHead.h" 


@implementation BeerDetailHead 

@synthesize beerName, beerImage; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     //beerName = [[UILabel alloc]init]; 
     //beerName.textAlignment = UITextAlignmentLeft; 
     //beerName.font = [UIFont systemFontOfSize:14]; 
     beerImage = [[UIImageView alloc]init]; 
     //[self.contentView addSubview:beerName]; 
     [self.contentView addSubview:beerImage]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 
    frame= CGRectMake(boundsX+10 ,10, 280, 375); 
    beerImage.frame = frame; 
} 

DetailViewController의 cellForRowAtIndexPath 대한 구현

#import <UIKit/UIKit.h> 


@interface BeerDetailHead : UITableViewCell { 
    UILabel *beerName; 
    UIImageView *beerImage; 
} 

@property(nonatomic, retain)UILabel *beerName; 
@property(nonatomic, retain)UIImageView *beerImage; 

@end 

주요부 tableView:heightForRowAtIndexPath: 델리게이트 메소드를 구현하고 계산 된 높이를 반환

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.tableContents objectForKey: 
         [self.sortedKeys objectAtIndex:[indexPath section]]]; 

    NSLog(@"listData = %@", listData); 

    NSUInteger row = [indexPath row]; 

    if ([self.sortedKeys objectAtIndex:[indexPath section]] == @"header"){ 
     static NSString *headerTableIdentifier = @"HeaderTableIdentifier"; 
     BeerDetailHead * headerCell = (BeerDetailHead*)[tableView 
                 dequeueReusableCellWithIdentifier: headerTableIdentifier]; 

     if(headerCell == nil) { 

      headerCell = [[[BeerDetailHead alloc] initWithFrame:CGRectZero reuseIdentifier:headerTableIdentifier] autorelease];     
     } 

     headerCell.beerName.text = [listData objectAtIndex:row]; 
     [headerCell.beerImage setImageWithURL:[NSURL URLWithString:self.beerPhoto] 
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
     //NSLog(@"frame = %@", headerCell.beerImage.frame); 

     return headerCell; 
    } 
    else{ 
     //use standard UITableViewCell 
    } 
} 

답변

1

이 메소드의 각 행
당신의있는 tableView에 각각의 이미지 호출 reloadData를로드 한 후 또는 변경 사항은 전화 애니메이션을 적용 할 경우 : 또한

[self.tableView beginUpdates]; 
[self.tableView endUpdates]; 

, 당신은 하나에 여러 개의 시퀀스 높이 업데이트를 결합 할 수 있습니다. 나는 이것을 수행하는데 약간의 지연을 사용할 것이다 :

// triggerUpdates calls the above code 
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(triggerUpdates) object:nil]; 
[self performSelector:@selector(triggerUpdates) withObject:nil afterDelay:0.1]; 
+0

나는 상수로 구현 된 tableView : heightForRowAtIndexPath를 가졌다. 여기에 그것을 깜빡 했네. 지금 현재 맞춤 UITableViewCell은 이미지 너비, 높이 및 상수 크기가 조정 된 너비 280을 사용하여 UIImageView 프레임의 올바른 높이를 계산합니다. viewDidAppear에서 reloadData를 호출하면 올바른 이미지가 제공됩니다. 그러나 heightForRowAtIndexPath는 이미지의 높이가 정확하기 전에 계산됩니다. 높이에 대한 적절한 해결책을 찾으십시오. – scottoliver

+0

왜 궁금해서 조금 지연 되었습니까? 특별한 이유가 있습니까? – fatih