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

时间:2011-11-12 05:09:32

标签: iphone objective-c ios xcode uitableview

  

可能重复:
  Alternative to switch statement in objective C

我有一个url的json数据,我使用switch语句显示在表格单元格中。由于我只有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;
            }

3 个答案:

答案 0 :(得分:2)

这是一种彻底解决问题的方法,但你可能会在那里找到一些有用的小块来帮助解决你的具体问题:

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 :(得分:2)

另一种过度设计的选项,你可以从中窃取一些金块是基于对象的方法:

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

答案 2 :(得分:1)

您可以做的事情是制作某种数据对象,其中包含您需要在stringWithFormat方法中显示的信息。

然后创建一个NSDictionary,键值将这些对象与索引对...

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