iOS中使用UItableviewcell实现团购和微博界面的示例

2020-01-14 18:26:55于海丽

        cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject];
        NSLog(@"创建了一个cell");
    }
    
    //设置cell的数据
    //获取当前行的模型
    YYtg *tg=self.tg[indexPath.row];
    cell.yytg=tg;
    return cell;
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

@end


3.对上述代码进行进一步的优化和调整(MVC)

 

优化如下:

(1)把主控制器中创建cell的过程抽取到YYtgcell中完成,并对外提供一个接口。

YYtgcell.h文件(提供接口)

复制代码
#import <UIKit/UIKit.h>
#import "YYtgModel.h"

 

@interface YYtgcell : UITableViewCell
@property(nonatomic,strong)YYtgModel *yytg;

//把加载数据(使用xib创建cell的内部细节进行封装)
+(instancetype)tgcellWithTableView:(UITableView *)tableView;
@end


YYtgcell.m文件(把创建自定义cell的部分进行封装)
复制代码
//
//  YYtgcell.m
//  02-团购(使用xib和类完成数据展示)
//
//  Created by apple on 14-5-29.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYtgcell.h"
//私有扩展
@interface YYtgcell()
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *titlelab;
@property (strong, nonatomic) IBOutlet UILabel *pricelab;
@property (strong, nonatomic) IBOutlet UILabel *buycountlab;
@end


复制代码
@implementation YYtgcell

 

#pragma mark 重写set方法,完成数据的赋值操作
-(void)setYytg:(YYtgModel *)yytg
{
    _yytg=yytg;
    self.img.image=[UIImage imageNamed:yytg.icon];
    self.titlelab.text=yytg.title;
    self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price];