iOS学习08代码1

来源:互联网 发布:电子设备审计软件功能 编辑:程序博客网 时间:2024/06/06 20:43
一个自定义cell的小例子
group-Control
//  //  ViewController.m//  1105-cellxib////  Created by 修修 on 15/11/5.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import "ViewController.h"#import "SLTgModel.h"#import "SLTgViewTableViewCell.h"#import "SLFootView.h"@interface ViewController () <UITableViewDataSource, SLFootViewDelegate>@property (weak, nonatomic) IBOutlet UITableView *tableView;/** *  保存模型数据的数组 */@property (nonatomic, strong) NSMutableArray *tgModels;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // tableView每一行的高度    self.tableView.rowHeight = 80;    // 设置tableView底部的tableFooterView    // 自定义一个xib文件,并为xib绑定自己的类,在自定义的类中读取xib并返回一个footview对象    // 将这个对象赋值给tableFooterView    // 由于要监听自定义footview的按钮事件,并让控制器加载新的数据,所以要自定义的类写一个自定义的代理    // 并让控制器称为他的代理,来告诉控制器我什么时候点击按钮,并让控制器做些事情    // 这就是一种代理设计模式    SLFootView *footview = [SLFootView footview];    footview.delegate = self;    self.tableView.tableFooterView = footview;}/** *  隐藏手机顶部的状态栏(运营商、电量...) * *  @return yes:隐藏,no:不隐藏 */-(BOOL)prefersStatusBarHidden{    return YES;}#pragma mark - getter方法的重写- (NSMutableArray *)tgModels{    // 懒加载:有值的时候就不再赋值直接返回,没有值的时候才进行赋值    if (_tgModels == nil) {        // 通过mainBundle拿到plist的全路径        NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];        // 取得plist中的数组        NSArray *tempTgs = [NSArray arrayWithContentsOfFile:path];        // 定义一个可变数组,用来临时存储模型数据        NSMutableArray *tempMTgs = [NSMutableArray array];        // 遍历plist数组中的字典        for ( NSDictionary *dict in tempTgs )        {            // 自定义一个模型类,将字典传入,得到相应的模型(实现了很好地封装性)            SLTgModel *tgModel = [SLTgModel tgModelWithDict:dict];            // 将模型加到之前的临时可变数组中            [tempMTgs addObject:tgModel];        }        // 将临时数组赋给我们定义好的模型数组        _tgModels = tempMTgs;    }    // 返回数组    return _tgModels;}#pragma mark - 数据源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 根据模型数组中元素的个数返回tableView的行数    return self.tgModels.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 生成cell    // 我们通过xib自定义了一个cell,并让他写一个类SLTgViewTableViewCell,在这个类中管理我们自定义的cell    // 控制器只要给我相应的tableView,我就给他返回一个我们自己写的cell    SLTgViewTableViewCell *cell = [SLTgViewTableViewCell tgViewTableViewCell:tableView];    // 给cell添加数据    // 将模型传给我们的cell,在cell中重写模型的setter方法,并把模型的值赋给相应的控件    cell.tgModel = self.tgModels[indexPath.row];    // 返回cell,这个cell中的控件已经有了相应的值    return cell;}#pragma mark - footview的代理方法// 当footview中的loadbtn发生点击事件的时候,控制器会自动调用这个方法- (void)footviewDidClickedLoadBtn:(SLFootView *)footview{    // 真正的项目中,是从网络上获取数据,在这就模拟一下这个过程    SLTgModel *tgModel = [[SLTgModel alloc] init];    tgModel.icon = @"2010e3a0c7f88c3f5f5803bf66addd93";    tgModel.title = @"金钱豹";    tgModel.price = @"888";    tgModel.buyCount = @"10000";    [self.tgModels addObject:tgModel];    [self.tableView reloadData];}@end
group-View
////  SLTgViewTableViewCell.h//  1105-cellxib////  Created by 修修 on 15/11/5.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import <UIKit/UIKit.h>@class SLTgModel;@interface SLTgViewTableViewCell : UITableViewCell/** *  每个cell对应的模型 */@property (nonatomic, strong) SLTgModel *tgModel;+ (instancetype)tgViewTableViewCell:(UITableView *)tableView;@end////  SLTgViewTableViewCell.m//  1105-cellxib////  Created by 修修 on 15/11/5.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import "SLTgViewTableViewCell.h"#import "SLTgModel.h"@interface SLTgViewTableViewCell ()/** *  店家图片 */@property (weak, nonatomic) IBOutlet UIImageView *iconView;/** *  购买数量 */@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;/** *  商品价格 */@property (weak, nonatomic) IBOutlet UILabel *priceLabel;/** *  店家名称 */@property (weak, nonatomic) IBOutlet UILabel *titleLabel;@end@implementation SLTgViewTableViewCell// 重写模型的setter方法,并将模型的值赋给相对应的控件- (void)setTgModel:(SLTgModel *)tgModel{    _tgModel = tgModel;    self.iconView.image = [UIImage imageNamed:tgModel.icon];    self.titleLabel.text = tgModel.title;    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", tgModel.price];    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人购买", tgModel.buyCount];}// 将我们生成cell的方法封装起来,只提供一个生成cell的方法+ (instancetype)tgViewTableViewCell:(UITableView *)tableView{    // cell 的唯一标识    NSString *ID = @"tg";    // 在tableview的缓存池找对应ID的cell    SLTgViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    if (cell == nil) { // 没有对应的cell,就通过读取xib拿到我们的cell        cell = [[[NSBundle mainBundle] loadNibNamed:@"SLTg" owner:nil options:nil] lastObject];    }    return cell;}@end

////  SLFootView.h//  1105-cellxib////  Created by 修修 on 15/11/6.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import <UIKit/UIKit.h>@class SLFootView;// SLFootView的代理协议@protocol SLFootViewDelegate <NSObject>// 下面的方法,代理是可以选择实现的@optional// 当SLFootView中的LoadBtn发生点击的时候,就会通知代理调用该方法- (void)footviewDidClickedLoadBtn:(SLFootView *)footview;@end@interface SLFootView : UIView/** *  外部用来初始化SLFootView的接口 * *  @return 放回相应的SLFootView对象 */+ (instancetype)footview;/** *  SLFootView的代理,id<SLFootViewDelegate> :表示任何对象都可以成为我们的代理,但要遵守相应的协议 */@property (nonatomic, strong) id<SLFootViewDelegate> delegate;@end////  SLFootView.m//  1105-cellxib////  Created by 修修 on 15/11/6.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import "SLFootView.h"@interface SLFootView ()/** *  加载更多按钮 */@property (weak, nonatomic) IBOutlet UIButton *loadBtn;/** *  正在加载的时候要显示的view */@property (weak, nonatomic) IBOutlet UIView *loadingView;/** *  加载更多按钮的点击事件 */- (IBAction)loadBtnClick;@end@implementation SLFootView+ (instancetype)footview{    // 通过xib返回相应的footview    return [[[UINib nibWithNibName:@"SLFootView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];}- (IBAction)loadBtnClick{    // 加载按钮隐藏,正在加载的view显示    self.loadBtn.hidden = YES;    self.loadingView.hidden = NO;    // 一定的延迟(模拟网络加载用的那一段时间)delayInSeconds:几秒后,执行block中的代码    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        // 如果代理实现了相应的方法,就通知代理,并把自己传过去,告诉代理是谁发生了什么事        if ([self.delegate respondsToSelector:@selector(footviewDidClickedLoadBtn:)]) {              [self.delegate footviewDidClickedLoadBtn:self];        }        // 加载按钮显示,正在加载的view隐藏        self.loadBtn.hidden = NO;        self.loadingView.hidden = YES;    });}@end
group-Model
<pre name="code" class="objc">////  SLTgModel.m//  1105-cellxib////  Created by 修修 on 15/11/5.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import "SLTgModel.h"@implementation SLTgModel- (instancetype)initWithDict:(NSDictionary *)dict{    if (self = [super init]) {        // 通过KVC方式赋值        [self setValuesForKeysWithDictionary:dict];    }    return self;}+ (instancetype)tgModelWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}@end////  SLTgModel.m//  1105-cellxib////  Created by 修修 on 15/11/5.//  Copyright © 2015年 cn.Xsoft. All rights reserved.//#import "SLTgModel.h"@implementation SLTgModel- (instancetype)initWithDict:(NSDictionary *)dict{    if (self = [super init]) {        // 通过KVC方式赋值        [self setValuesForKeysWithDictionary:dict];    }    return self;}+ (instancetype)tgModelWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}@end
                                             
0 0
原创粉丝点击