iOS开发总结之自定义等高cell-storyboard

1.storybord中添加子控件并设置tag

iOS开发总结之自定义等高cell-storyboard_第1张图片

2.代码:

#import "XMGDealsViewController.h"
#import "XMGDeal.h"

@interface XMGDealsViewController ()
/** 所有的团购数据 */
@property (nonatomic, strong) NSArray *deals;
@end

@implementation XMGDealsViewController

- (NSArray *)deals
{
    if (_deals == nil) {
        // 加载plist中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *dealArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            XMGDeal *deal = [XMGDeal dealWithDict:dict];
            [dealArray addObject:deal];
        }
        
        _deals = dealArray;
    }
    return _deals;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"deal";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 取出模型数据
    XMGDeal *deal = self.deals[indexPath.row];
    
    // 设置数据
    UIImageView *iconView = (UIImageView *)[cell viewWithTag:10];
    iconView.image = [UIImage imageNamed:deal.icon];
    
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:20];
    titleLabel.text = deal.title;
    
    UILabel *priceLabel = (UILabel *)[cell viewWithTag:30];
    priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    
    UILabel *buyCountLabel = (UILabel *)[cell viewWithTag:40];
    buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
    
    return cell;
}

@end

3.效果:

iOS开发总结之自定义等高cell-storyboard_第2张图片

你可能感兴趣的:(iOS开发总结之自定义等高cell-storyboard)