2013-07-17 8 views
6

メインビューで呼び出される行に5つのイメージがあるカスタムセルFeatureCellを作成しましたが、それを呼び出すと空の行が表示されます。だから私の問題がどこにありますか?1つのUITableViewの問題で2つの異なるカスタムセルを呼び出す

私はカスタムセルについて調べていますが、私は以下のコードで使用しなければならない方法を使用しましたが、何も起こりません。

この

は、これは私の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

あなたはこのような何かを実行する必要があります。

- (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; 
    } 
} 

私は後方そうif条件を持っていることを再確認することがあります。

+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; 

} 
関連する問題