2013-07-17 2 views
6

주 뷰에서 호출 될 행에 5 개의 이미지가있는 사용자 정의 셀 FeatureCell을 만들었지 만 호출 할 때 빈 행이 표시됩니다. 그래서 내 문제가 될 수있는 곳으로?하나의 UITableView 문제에서 두 개의 다른 사용자 정의 셀 호출

나는 사용자 정의 셀에 대해 봤지만 나는 아래 코드에서 사용해야하지만 아무 일도 일어나지 않았다.

이 내 FeatureCell.m 내 FeatureCell.h

@interface FeatureCell : UITableViewCell{ 

    IBOutlet UIImageView *img1; 
    IBOutlet UIImageView *img2; 
} 

@property (nonatomic, retain) UIImageView *img1; 
@property (nonatomic, retain) UIImageView *img2; 
@end 

입니다

@implementation FeatureCell 

@synthesize img,img1,img2,img3,img4; 
@end 

이것은 내보기 컨트롤러이다하는 .m

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 

return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

if (section == 0) { 
    return [objectCollection count]; 
} 
else{ 
    return 1; 
    } 
}  

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

static NSString* cellIdentifier1 = @"FeatureCell"; 

FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 


if (cell1 == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 

    cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
} 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
            reuseIdentifier:CellIdentifier]; 

} 


if ([indexPath section] == 0) { 

    containerObject = [objectCollection objectAtIndex:indexPath.row]; 

    [[cell textLabel] setText:containerObject.store]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

} 
    else{ 

    cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
    cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

    } 
     return cell; 
} 
+0

당신이 콘센트를 연결 했 내 문제 해결? – Wain

+0

이것은 2 가지 다른 세포 유형을 다루는 올바른 방법이 아닙니다. 내 대답보기 : http://stackoverflow.com/questions/17711452/change-uicollectionviewcell-content-and-nib-layout-based-on-data/17711644#17711644. 컬렉션 뷰를 다루지 만 테이블 뷰에서도 마찬가지입니다. – rdelmar

+0

@Wain 콘센트는 무엇을 의미합니까? –

답변

13

당신은 같은 것을 할 필요가 : 나는 그것을 확인 뒤로 그래서 두 번 if 조건이있을 수 있습니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     static NSString *CellIdentifier = @"Cell"; 

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

     containerObject = [objectCollection objectAtIndex:indexPath.row]; 
     [[cell textLabel] setText:containerObject.store]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     return cell; 
    } else { 
     static NSString* cellIdentifier1 = @"FeatureCell"; 

     FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 == nil) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 
      cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
     } 

     cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
     cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

     return cell1; 
    } 
} 

.

+0

cellForRowAtIndexPath는 UITableViewCell 인스턴스를 반환해야합니다. if-else 문 밖에서 아무 것도 반환하지 않기 때문에 코드에서 오류가 발생합니다. – Sebyddd

+0

@Sebyddd 아니요. 괜찮습니다. – rmaddy

+0

'제어가 비공개 기능의 끝 부분에 도달 할 수 있습니다.' :) – Sebyddd

0

이 구현 :

@implementation FeatureCell 
@synthesize img,img1,img2,img3,img4; 
@end 

일부 접근 자 메소드가 있지만 초기화 된 인스턴스가없는 셀을 만듭니다. 인스턴스를 작성하거나 변수 (아울렛으로 정의)를 연관된 XIB 파일에 연결하려면 init 메소드를 구현해야합니다. this guide을 읽으십시오.

다른 셀 인스턴스를 어떻게 작성하는지에 대한 의견에 동의합니다. 그것은 매우 혼란스럽고 참조를 뒤섞는 것 같습니다. 셀 인스턴스 이름을 입력 할 수 없도록 여러 가지 방법으로 셀을 분할하거나 적어도 if 문 안에 셀을 작성하는 코드를 넣어야합니다.

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

{ 경우 (indexPath.row == 0) //하지 indexPath.section은 ...

{ 
    static NSString *CellIdentifier = @"MenuHeadingCustomCell"; 

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

    return cell; 
} 





else 
    { 
     static NSString* cellIdentifier1 = @"LevelViewCell"; 
     LevelViewCell *cell1 =[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 ==nil) 
     { 
      cell1 = [[LevelViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier1]; 

     // row counts which dictionary entry to load: 
     _Dictionary=[_array objectAtIndex:indexPath.row]; 

     cell1.LevelTitleText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelText"]]; 
     cell1.LevelSubText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelSubText"]]; 
     cell1.LevelThumb.image =[UIImage imageNamed:[_Dictionary objectForKey:@"LevelLabelThumb"]]; 

    } 
    return cell1; 

} 
관련 문제