2011-08-24 2 views
0

나는 사용자 정의 UITableViewCell 클래스를 가지고 있으며 이미지와 문자열을 선형으로 표시하려고합니다.서브 클래 싱 된 UITableViewCell 내에 동적으로 생성 된 서브 뷰

행 1 : 예 [이미지 1] 문자열 1 [이미지 2] 문자열 2 [이미지 3]

행 2 : 이미지 4] string3 [이미지 5]

이미지가 변화 한 폭하지만 균등 간격을 원합니다. 어떻게하면 좋을까요? 나는 subviews를 조작하고 CGRectMake 아무 소용이 시도했다.

또한 콘텐츠를 보유하기 위해 NSDictionary을 사용하고 있습니다. 이미지 수/문자열은 각 셀에 대해 일정하지 않습니다.

CustomCell 클래스 :

#import "CustomCell.h" 


@implementation CustomCell 

@synthesize primaryLabel,secondaryLabel,image1; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 
     // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:16]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:14]; 
     image1 = [[UIImageView alloc]init]; 

     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:image1]; 

    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect frame; 
    frame= CGRectMake(0 ,5, 200, 25); 
    primaryLabel.frame = frame; 

    frame= CGRectMake(0 ,30, 200, 25); 
    secondaryLabel.frame = frame; 

    frame= CGRectMake(0, 60, 23, 20); 
    image1.frame = frame; 

...

RootViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Set up the cell... 
    NSDictionary *dictionary = nil; 


//Search 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     dictionary = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
    } 


//Original 
    cell.primaryLabel.text = [dictionary objectForKey:@"Title"];  


    for (NSArray *keystroke in [dictionary objectForKey:@"Strokes"]) { 


      for (int i = 0; i < 2; i++) { 


       if ([(NSString *)keystroke isEqualToString:@"string1"] || [(NSString *)keystroke isEqualToString:@"string2"]) { 
       cell.secondaryLabel.text = (NSString *)keystroke; 
       } 

       else { 
      NSString *imageFilePath = [NSString stringWithFormat:@"%@.png", keystroke]; 
      NSLog(@"%@", imageFilePath); 
      UIImage *myimage = [UIImage imageNamed:imageFilePath]; 

       cell.image1.image = myimage; 

     } 
     } 
    } 
    return cell; 


} 

...

분명히 여기에 구멍이 많이있다. 주로 사전을 반복 할 때 주로 이전 서브 뷰 옆에 이미지/텍스트를 배치 할 수 있도록 내 CustomCell 하위 뷰를 이동해야합니다.

답변

0

당신은 올바른 길을 가고 있습니다. UITableViewCell 하위 클래스에서 layoutSubviews을 무시하고 각 UIImageView 또는 UILabel에 대해 CGRects을 수동으로 정의하고 해당보기의 프레임으로 설정해야합니다.

체크 아웃 CGRectGetMaxX 이 문맥에서 매우 유용 할 것입니다.

+0

아직 잃어 버렸기 때문에 몇 가지 코드를 추가했습니다. 답장을 보내 주셔서 감사합니다! –

관련 문제