我可以使用什么替代switch语句来更新我的UITableViewCells?

可能重复:
替代目标C中的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; } 

这里有一个方法可以彻底地解决这个问题,但是你可能会在那里find一些有用的东西,这些东西可以帮助你解决你的具体问题:

 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 

另一个过度工程的select,你可能能够窃取一些掘金是基于对象的方法:

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 

你可以做的事情是做一些数据objet包含你需要显示在你的stringWithFormat方法的信息。

然后做一个NSDictionary键值对这些对象与索引…

但在这个特殊情况下,所有这些工作是否值得?
我对此表示怀疑。