2011-11-12 26 views
2

可能性の重複:
Alternative to switch statement in objective C私は自分のUITableViewCellを更新するためにswitch文の代わりに何を使用できますか?

私はswitch文を使用して、私は表のセルに表示されたURLからJSONデータを持っています。私は6つのセルしか持っていないのでswitch文を使っていましたが、switch文の代わりに他の方法があるかどうか不思議です。

switch(indexPath.row) 
    { 
     case 0 : 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[dictionary valueForKey:@"firstname"], 
            [dictionary valueForKey:@"lastname"]]; 
      break; 

     case 1: 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Address",[dictionary valueForKey:@"address"]]; 
      break; 

     case 2: 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Age",[dictionary valueForKey:@"age"]]; 
      break; 

     case 3: 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Occupation",[dictionary valueForKey:@"occupation"]]; 
      break; 

     case 4: 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Rating",[rate valueForKey:@"average"]]; 
      break; 

     case 5: 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Total Rating",[rate valueForKey:@"totalRatings"]]; 
      break; 
      } 
+0

あなたが例を提供することができますか? –

+0

私はコードを追加しました。あなたは私を案内してもらえますか – iDev

+0

switchステートメントのそれぞれのケースが異なるロジック(特にケース4)を使用するため、これに対する絶対的な簡単な解決法はありません。 –

答えて

1

あなたができることは、stringWithFormatメソッドで表示する必要がある情報を含む何らかのデータオブジェクトを作成することです。

そしてNSDictionaryのキーと値のペアそのインデックスを持つものを対象にする...

しかし、この特定のケースではそれだけの価値はすべてその仕事ですか?
私はそれを疑う。

2

ここで完全に問題overengineerへの道ですが、あなたはあなたの特定の問題のお手伝いをしますそこにいくつかの有用なナゲットを見つけることがあります。

typedef enum MONTableViewCellID { 
    MONTableViewCellID_Name = 0, 
    MONTableViewCellID_Address, 
    MONTableViewCellID_Age, 
    MONTableViewCellID_Occupation, 
    MONTableViewCellID_Rating, 
    MONTableViewCellID_TotalRating 
} MONTableViewCellID; 

@interface MONTableViewStuff : NSObject 
{ 
@private 
    NSDictionary * dictionary; 
    NSDictionary * rate; 
    UITableViewCell * cell; 
    NSIndexPath * indexPath; 
} 
@end 

@implementation MONTableViewStuff 

- (UITableViewCellAccessoryType)tableViewAccessoryTypeForRow:(NSUInteger)row 
{ 
    if (MONTableViewCellID_Rating == row) { 
     return UITableViewCellAccessoryDisclosureIndicator; 
    } 
    return UITableViewCellAccessoryNone; 
} 

- (NSString *)lhsTextForRow:(NSUInteger)row 
{ 
    switch (row) { 
     case MONTableViewCellID_Name : 
      return [dictionary objectForKey:@"firstname"]; 
     case MONTableViewCellID_Address : 
      return @"Address"; 
     case MONTableViewCellID_Age : 
      return @"Age"; 
     case MONTableViewCellID_Occupation : 
      return @"Occupation"; 
     case MONTableViewCellID_Rating : 
      return @"Rating"; 
     case MONTableViewCellID_TotalRating : 
      return @"Total Rating"; 
     default : { 
      assert(0 && "invalid row"); 
      return @""; 
     } 
    } 
} 

- (NSString *)rhsTextForRow:(NSUInteger)row 
{ 
    switch (row) { 
     case MONTableViewCellID_Name : 
      return [dictionary objectForKey:@"lastname"]; 
     case MONTableViewCellID_Address : 
      return [dictionary objectForKey:@"address"]; 
     case MONTableViewCellID_Age : 
      return [dictionary objectForKey:@"age"]; 
     case MONTableViewCellID_Occupation : 
      return [dictionary objectForKey:@"occupation"]; 
     case MONTableViewCellID_Rating : 
      return [rate objectForKey:@"average"]; 
     case MONTableViewCellID_TotalRating : 
      return [rate objectForKey:@"totalRatings"]; 
     default : { 
      assert(0 && "invalid row"); 
      return @""; 
     } 
    } 
} 

- (NSString *)separatorForRow:(NSUInteger)row 
{ 
    switch (row) { 
     case MONTableViewCellID_Name : 
      return @" "; 
     case MONTableViewCellID_Address : 
     case MONTableViewCellID_Age : 
     case MONTableViewCellID_Occupation : 
     case MONTableViewCellID_Rating : 
     case MONTableViewCellID_TotalRating : 
      return @" : "; 
     default : { 
      assert(0 && "invalid row"); 
      return @""; 
     } 
    } 
} 


- (NSString *)textLabelTextForRow:(NSUInteger)row 
{ 
    return [NSString stringWithFormat:@"%@%@%@", [self lhsTextForRow:row], [self separatorForRow:row], [self rhsTextForRow:row]]; 
} 

- (void)updateTextLabel 
{ 
    cell.textLabel.text = [self textLabelTextForRow:indexPath.row]; 
    cell.accessoryType = [self tableViewAccessoryTypeForRow:indexPath.row]; 
} 

@end 
+1

それを愛する。もっとシングルトンが必要です;) – jrturton

+0

@jrturton =)ああ、はい!シングルトンの答えはメールにあります;) – justin

+1

あなたのメソッドのシグネチャにはなぜNSUIntegerを使用しますか? 'MONTableViewCellID'はもう少し自己文書化されませんか? –

2

あなたには、いくつかのナゲットを盗むことができるかもしれ別overengineeredオプションオブジェクトベースのアプローチである:

MONTableViewStuff.h

typedef enum MONTableViewCellID { 
    MONTableViewCellID_Name = 0, 
    MONTableViewCellID_Address, 
    MONTableViewCellID_Age, 
    MONTableViewCellID_Occupation, 
    MONTableViewCellID_Rating, 
    MONTableViewCellID_TotalRating 
} MONTableViewCellID; 

@interface MONTableViewStuff : NSObject 

@property (nonatomic, copy, readonly) NSDictionary * dictionary; 
@property (nonatomic, copy, readonly) NSDictionary * rate; 

@end 

MONTableViewStuff.m

プレゼンターインタフェースは、ように見えた
@implementation MONTableViewStuff 

@synthesize dictionary; 
@synthesize rate; 

- (id)init 
{ 
    self = [super init]; 
    if (0 != self) { 
     /* create an array of presenters ordered by MONTableViewCellID */ 
     presenters = 
      [NSArray arrayWithObjects: 
      [[NamePresenter new] autorelease], 
      [[AddressPresenter new] autorelease], 
      [[AgePresenter new] autorelease], 
      [[OccupationPresenter new] autorelease], 
      [[RatingPresenter new] autorelease], 
      [[TotalRatingPresenter new] autorelease], 
      nil 
     ]; 
    } 
    return self; 
} 

- (void)updateTableViewCell 
{ 
    NSObject<MONUITableViewCellPresenter>* presenter = [presenters objectAtIndex:indexPath.row]; 
    [presenter updateUITableViewCell:cell tableViewStuff:self]; 
} 

@end 

@protocol MONUITableViewCellPresenter <NSObject> 
@required 
- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff; 
@end 

// our base presenter which handles the cells 
@interface DefaultPresenter : NSObject 

/** @return UITableViewCellAccessoryNone */ 
- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff; 

- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff; 

@end 

// required overrides 
@protocol DefaultPresenterSubclass <MONUITableViewCellPresenter> 
@required 
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff; 
@end 

// our specializations 
@interface NamePresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

@interface AddressPresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

@interface AgePresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

@interface OccupationPresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

@interface RatingPresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

@interface TotalRatingPresenter : DefaultPresenter <DefaultPresenterSubclass> 
@end 

そしてその実装そうに見えた:

@implementation DefaultPresenter 

- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
#pragma unused (tableViewStuff) 
    return UITableViewCellAccessoryNone; 
} 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
#pragma unused (tableViewStuff) 
    assert(0 && "specialization required"); 
    return 0; 
} 

- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    cell.accessoryType = [self cellAccessoryTypeForTableViewStuff:tableViewStuff]; 
    cell.textLabel.text = [self cellTextForTableViewStuff:tableViewStuff]; 
} 

@end 

@implementation NamePresenter 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ %@",[tableViewStuff.dictionary valueForKey:@"firstname"], [tableViewStuff.dictionary valueForKey:@"lastname"]]; 
} 

@end 

@implementation AddressPresenter 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ : %@",@"Address",[tableViewStuff.dictionary valueForKey:@"address"]]; 
} 

@end 

@implementation AgePresenter 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ : %@",@"Age",[tableViewStuff.dictionary valueForKey:@"age"]];; 
} 

@end 

@implementation OccupationPresenter 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ : %@",@"Occupation",[tableViewStuff.dictionary valueForKey:@"occupation"]]; 
} 

@end 

@implementation RatingPresenter 

+ (UITableViewCellAccessoryType)cellAccessoryType 
{ 
    return UITableViewCellAccessoryDisclosureIndicator; 
} 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ : %@",@"Rating",[tableViewStuff.rate valueForKey:@"average"]]; 
} 

@end 

@implementation TotalRatingPresenter 

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff 
{ 
    return [NSString stringWithFormat:@"%@ : %@",@"Total Rating",[tableViewStuff.rate valueForKey:@"totalRatings"]]; 
} 

@end 
関連する問題