我可以用什么替代switch语句来更新我的UITableViewCells? [英] What alternatives to a switch statement could I use to update my UITableViewCells?

查看:76
本文介绍了我可以用什么替代switch语句来更新我的UITableViewCells?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

替换目标C中的switch语句

我有一个url的json数据,我使用switch语句显示在表格单元格中。由于我只有6个单元格,所以我使用了switch语句,但我很想知道是否还有其他方法来代替switch语句。

I have json data from an url which I display in the table cell using a switch statement. Since I have only 6 cells, I had used a switch statement but I am curious to know if there is any other method in place of switch statement to do so.

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


推荐答案

您可能选择另一种过度设计的选项能够从基于对象的方法中窃取一些金块:

Another overengineered option that you may be able to steal some nuggets from is the object based approach:

MONTableViewStuff.h

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

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

演示者界面的位置像这样:

Where the presenters' interface looked like so:

@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

他们的实现看起来像这样:

And their implementations looked like so:

@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

这篇关于我可以用什么替代switch语句来更新我的UITableViewCells?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆