あなたには、いくつかのナゲットを盗むことができるかもしれ別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
あなたが例を提供することができますか? –
私はコードを追加しました。あなたは私を案内してもらえますか – iDev
switchステートメントのそれぞれのケースが異なるロジック(特にケース4)を使用するため、これに対する絶対的な簡単な解決法はありません。 –