2012-05-05 2 views
2

많은 검색을 해왔지만 여러 사용자 정의 행과 관련된 유용한 것을 찾지 못했습니다. x30 파일에서 행을로드해야하는 내 앱의 설정 tableView를 만들어야합니다.여러 사용자 정의 행 UITableView?

ROW 1 = 1 >> XIB
ROW 2 = 2 >> XIB
ROW 3 = 3 >> XIB
ROW 4 = 4 >> XIB

내 본 번호 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell=nil; 
    //We use CellType1 xib for certain rows 
    if(indexPath.row==0){ 
     static NSString *CellIdentifier = @"ACell"; 
     cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell==nil){ 
      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil]; 
      cell = (ACell *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     //[cell.customLabelA setText:@"myText"] 
    } 
    //We use CellType2 xib for other rows 
    else{ 
     static NSString *CellIdentifier = @"BCell"; 
     cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell==nil){ 
      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil]; 
      cell = (BCell *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     //[cell.customLabelB setText:@"myText"] 
    } 

    return cell; 
} 

답변

7

먼저 당신이, 당신이 XIB 파일이있는만큼을 일부 사용자 지정있는 UITableViewCell 클래스 (.H와하는 .m)를 만들 수 있습니다.
CellType1.h은, 그리고 당신이 XIB 파일을 만들

#import <UIKit/UIKit.h> 
@interface CellType1 : UITableViewCell 

@property(nonatomic,strong) IBOutlet UILabel *customLabel; 

@end 

과 같을 것이다 당신은 기본보기 형식을 사용하지만, 단지 자동 생성 된 뷰를 제거하는있는 UITableViewCell에 의한 대체, 변경 수 CellType1에 ​​클래스. CellType2도 마찬가지입니다.

는 그런 다음 tableViewController 이와 같이 cellForRow 쓰기 :이 방법은 나를 위해 작동하지 않습니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell=nil; 
//We use CellType1 xib for certain rows 
if(indexPath.row==<whatever you want>){ 
    static NSString *CellIdentifier = @"CellType1"; 
    cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil){ 
     NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil]; 
     cell = (CellType1 *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     [cell.customLabel setText:@"myText"] 
} 
//We use CellType2 xib for other rows 
else{ 
    static NSString *CellIdentifier = @"CellType2"; 
    cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil){ 
     NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil]; 
     cell = (CellType2 *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     [cell.customLabel setText:@"myText"] 
} 

return cell; 
} 
+0

"<원하는 것은 무엇이든>"무엇을 의미합니까? – Mateus

+0

여러 개의 Xib을 사용하고 싶습니다. x1을 x1으로, xib2를 n2 등으로 추측하고 있습니다. 따라서 if/else 문은 행을 검사합니다 색인 및 달성하고자하는 것을 기준으로 올바른 행 색인 –

+0

에서 올바른 xib을 선택하십시오. 내 XIB의 기본보기는 UITableViewCells에 대해 변경해야합니까? 아니면보기의 크기를 조정해야합니까? – Mateus

3

xib에서 사용자 정의 셀을로드하는 데 익숙하지 않은 경우 documentation here을 확인하십시오. 이를 여러 사용자 정의 xib로 확장하려면 별도의 xib에 각 표 셀을 작성하고 고유 한 셀 ID를 부여하고 테이블보기 제어기를 파일의 소유자로 설정 한 다음 각 셀을 정의한 사용자 정의 셀 콘센트에 연결하십시오 (docs, 그들은이 콘센트로 tvCell를 사용합니다). 그런 다음 -tableView:cellForRowAtIndexPath: 메서드에서 셀을 제공하는 행을 확인하여 올바른 xib를로드하거나 올바른 재사용 가능한 셀을 dequeue 할 수 있습니다. 예를 들어 :
그래서 당신은 예를 들어 CellType1 및 CellType2을 할 수 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier1 = @"MyIdentifier1"; 
    static NSString *MyIdentifier2 = @"MyIdentifier2"; 
    static NSString *MyIdentifier3 = @"MyIdentifier3"; 
    static NSString *MyIdentifier4 = @"MyIdentifier4"; 

    NSUInteger row = indexPath.row 

    UITableViewCell *cell = nil; 

    if (row == 0) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer1]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell1" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 1) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer2]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell2" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 2) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer3]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell3" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 4) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer4]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell4" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    // etc. 

    // Do any other custom set up for your cell 

    return cell; 

} 
+0

. 나는 'UITableView dataSource tableView에서 셀을 반환해야합니다. : cellForRowAtIndexPath :'Thoughtts? ViewController를 각 셀에 연결하고 각 셀에 대해 사용자 정의 IBOutlet을 만들어 셀에 연결합니다. –

관련 문제