2014-07-07 4 views
1

뷰 컨트롤러 내부에 사용자 정의 셀이있는 테이블 뷰가 있습니다. 내 tableview 제대로 작동합니다. 내가하려는 것은 프로그래밍 방식으로 아래의 이미지를 개발하는 것이다. 여기서 "label"은 사용자 정의이며 일부 입력에 따라 변경되는 텍스트입니다. 이 두 레이블 (cellForRowAtIndexPath :)을 어떻게 포함하고 셀의 위치를 ​​결정할 수 있습니까? 이미지에 2 개의 다른 표 셀이 있습니다.동일한 테이블 셀에 두 개의 텍스트 레이블 (동적 테이블 하나와 정적 테이블 하나) 추가하기

Image contains 2 different table cells

나는 스토리 보드를 통해 작업을 수행하는 방법을 알고,하지만 난 엑스 코드 4.5 동적 세포를 사용하고 원인 나는 프로그래밍을 할 필요가있다.

이미지는 인덱스 셀 1과 2를 참조합니다. 지금까지는 셀당 하나의 텍스트 레이블 만 포함 할 수있었습니다. UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle :

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



static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 




    switch ([indexPath row]) 
    { 
     case 0: 
     { 
      // image cell - image resize and centred 


      UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)]; 
      imv.image=[UIImage imageNamed:@"test1.jpg"]; 


      imv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
      [cell.contentView addSubview:imv]; 

      imv.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2); 

      cell.accessoryType = UITableViewCellAccessoryNone; 

      break; 
     } 
     case 1: 
     { 


      cell.textLabel.text = @"Name"; 


      break; 
     } 

     case 2: 
     { 
      cell.textLabel.text = @"Manufacturer"; 

      break; 
     } 

     case 3: 
     { 
      cell.textLabel.text = @"Overall Score"; 

      break; 
     } 

     case 4: 
     { 
      cell.textLabel.text = @"Description"; 
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

      break; 
     } 

     case 5: 
     { 
      cell.textLabel.text = @"Videos"; 
      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

      break; 
     } 
      } 


    return cell; 

} 

사전에 기본적있는 UITableViewCell 스타일 첫번째

+0

사람들은 사용자가 모르는 부분을 알고 있기 때문에 시도한 것과 코드를 포함시켜야합니다. – believesInSanta

+0

감사합니다. 댓글. 나는 도움이 필요한 코드 부분을 추가했다. @believesInSanta – user3211165

+0

다른 사람들의 답변을 수락하거나 의견을 제공 할 수 있습니까? – believesInSanta

답변

1

확인을 주셔서 감사합니다.

사용할 수있는 경우 UITableViewCell의 하위 클래스를 만들 필요가 없습니다. 그렇지 않으면 UIView와 UILabel의 3 가지 속성을 내려야합니다. 그 이유는 기본 셀 스타일을 사용하지 않으면 셀 요소를 이동하거나 추가 할 수 없기 때문입니다.

다음 코드가 있어야 귀하있는 UITableViewCell 서브 클래스 : 기본적으로 방법 :

@interface UITableViewCellSubClass : UITableViewCell 
@property (nonatomic, strong) UIView *view; 
@property (nonatomic, strong) UILabel *label1; 
@property (nonatomic, strong) UILabel *label2; 
@end 

@implementation UITableViewCellSubClass 
@synthesize view; 
@synthesize label1; 
@synthesize label2; 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
if (self) { 
    view = [[UIView alloc] initWithFrame:self.frame]; 
    [self addSubview:view]; 
    // initiate label1 with position (10,10,150,20) 
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)]; 
    // initiate label2 with position (170,10,150,20) 
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)]; 
    [view addSubview:label1]; 
    [view addSubview:label2]; 
} 
return self; 
} 
@end 

그런 다음 당신은 당신의 cellForRowAtIndex에 그것을 반환 할 수이 도움이와 #을 잊지 마세요

UITableViewCellSubclass *cell = [[UITableViewCellSubclass alloc] initWithStyle:UITableViewCellDefault reuseIdentifier:@"cell"]; 
[cell.label1 setText:@"text"]; 
[cell.label2 setText:@"more text"]; 

희망을 "UITableViewCellSubClass.h"가져 오기

+0

고맙습니다. 늦게 답변을 수락했지만 죄송합니다. 다시 고마워. – user3211165

+0

내가 물어볼 게 또 있니? 모든 셀에 동일한 content/tableviewcell 하위 클래스가없는 것을 원한다면 cellForRowAtIndex를 통해 가장 쉬운 방법은 무엇입니까? – user3211165

+1

다른 재사용 식별자가있는 한 다른 tableviewcell 하위 클래스를 사용할 수도 있고 위치를 지정하는 유형 속성이있는 가능한 모든 UILabels, UIButtons 등을 직접 만들 수도 있습니다. – believesInSanta