首页 > Web开发 > 详细

美团网页面代码 UITableView

时间:2016-01-01 00:18:59      阅读:385      评论:0      收藏:0      [点我收藏+]

1、代码框架

技术分享

2、代码

Model:字典转模型

 1 //
 2 //  Tg.h
 3 //  美团tableVIewCell
 4 //
 5 //  Created by 刘羽 on 15/12/31.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Tg : NSObject
12 
13 @property (nonatomic, copy) NSString *title;
14 @property (nonatomic, copy) NSString *icon;
15 @property (nonatomic, copy) NSString *price;
16 @property (nonatomic, copy) NSString *buyCount;
17 
18 - (instancetype)initWithDict:(NSDictionary *)dict;
19 + (instancetype)tgWithDict:(NSDictionary *)dict;
20 
21 + (NSMutableArray *)tgs;
22 @end
 1 //
 2 //  Tg.m
 3 //  美团tableVIewCell
 4 //
 5 //  Created by 刘羽 on 15/12/31.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 
 9 #import "Tg.h"
10 
11 @implementation Tg
12 
13 -(instancetype)initWithDict:(NSDictionary *)dict
14 {
15     self = [super init];
16     if (self) {
17         [self setValuesForKeysWithDictionary:dict];
18     }
19     return self;
20 }
21 
22 +(instancetype)tgWithDict:(NSDictionary *)dict
23 {
24     return [[self alloc] initWithDict:dict];
25 }
26 
27 +(NSMutableArray *)tgs
28 {
29     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
30     NSMutableArray *arrayM = [NSMutableArray array];
31     for (NSDictionary *dict in array) {
32         [arrayM addObject:[self tgWithDict:dict]];
33     }
34     return arrayM;
35 }
36 
37 @end

2、Views: XIB

 1 //
 2 //  TgCell.h
 3 //  美团tableVIewCell
 4 //
 5 //  Created by 刘羽 on 15/12/31.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 #warning 这里设置此模型属性是为了接收外界传递进来的模型数据,有了外面的数据传入,才能具体的起到作用
 9 
10 #import <UIKit/UIKit.h>
11 #import <Foundation/Foundation.h>
12 @class Tg;
13 
14 //注意这里要和xib中的根节点保持一致 即继承自 UITableViewCell
15 @interface TgCell : UITableViewCell
16 /**定义团购的数据模型对象*/
17 @property(strong,nonatomic)Tg *tg;
18 //提高一个方法 可以快速创建cell
19 + (instancetype)cellWithTableView:(UITableView *)tableView;
20 
21 @end
//
//  TgCell.m
//  美团tableVIewCell
//
//  Created by 刘羽 on 15/12/31.
//  Copyright © 2015年 LX. All rights reserved.
//

#import "TgCell.h"
#import "Tg.h"
@interface TgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;

@end
@implementation TgCell


+(instancetype)cellWithTableView:(UITableView *)tableView
{
    //可重用标示符
    static NSString *ID = @"cell";
    //tableView查询可重用cell
    TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //没有就重新加载cell
    if (cell == nil) {
        //从xib加载自定义视图
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TgCell" owner:nil options:nil]lastObject];
        
    }
    return cell;
}

- (void)setTg:(Tg *)tg
{
    // setter方法中,第一句要赋值,否则要在其他方法中使用模型,将无法访问到
    _tg = tg;
    //tg就是Tg类的对象 该对象在TgCell的.h文件中被定义
    self.titleLabel.text = tg.title;
    self.iconView.image = [UIImage imageNamed:tg.icon];
    self.priceLabel.text = tg.price;
    self.buyCountLabel.text = tg.buyCount;
}

#pragma mark - 模板提供的方法
/**
 初始化方法
 
 使用代码创建Cell的时候会被调用,如果使用XIB或者Storyboard,此方法不会被调用
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSLog(@"%s", __func__);
    }
    return self;
}

/**
 从XIB被加载之后,会自动被调用,如果使用纯代码,不会被执行
 */
- (void)awakeFromNib
{
    NSLog(@"%s", __func__);
    self.contentView.backgroundColor = [UIColor clearColor];
}

/**
 Cell 被选中或者取消选中是都会被调用
 
 如果是自定义Cell控件,所有的子控件都应该添加到contentView中
 */
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        self.contentView.backgroundColor = [UIColor redColor];
    } else {
        self.contentView.backgroundColor = [UIColor greenColor];
    }
}



@end
 1 //
 2 //  TgFooterView.h
 3 //  美团tableVIewCell
 4 //
 5 //  Created by 刘羽 on 15/12/31.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 #import <UIKit/UIKit.h>
 9 
10 @class TgFooterView;
11 
12 @protocol TgFooterViewDelegate <NSObject>
13 
14 @optional
15 /** 视图的下载按钮被点击 */
16 - (void)tgFooterViewDidDownloadButtonClick:(TgFooterView *)footerView;
17 
18 @end
19 
20 @interface TgFooterView : UIView
21 
22 
23 // 代理如果使用强引用,就会产生循环引用,造成控制器和子视图都无法被释放,造成内存泄露
24 @property (nonatomic, weak) id <TgFooterViewDelegate> delegate;
25 
26 + (instancetype)footerView;
27 
28 /** 刷新数据结束后,更新页脚的视图显示 */
29 - (void)endRefresh;
30 
31 
32 @end
//
//  TgFooterView.m
//  美团tableVIewCell
//
//  Created by 刘羽 on 15/12/31.
//  Copyright © 2015年 LX. All rights reserved.
//

#import "TgFooterView.h"


@interface TgFooterView()
@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
@property (weak, nonatomic) IBOutlet UIView *tipsView;
@end

@implementation TgFooterView

+ (instancetype)footerView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"TgFooterView" owner:nil options:nil] lastObject];
}

- (IBAction)loadMore
{
    NSLog(@"加载更多");
    // 1. 隐藏按钮
    self.loadMoreButton.hidden = YES;
    // 2. 显示提示视图
    self.tipsView.hidden = NO;
    
    // 3.1 判断代理是否实现了协议方法
    if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownloadButtonClick:)]) {
        [self.delegate tgFooterViewDidDownloadButtonClick:self];
    }
}

/** 视图控制器刷新完成调用方法 */
- (void)endRefresh
{
    // 4. 加载完成数据
    self.loadMoreButton.hidden = NO;
    self.tipsView.hidden = YES;
}


@end

3、Contorllers

  1 //
  2 //  ViewController.m
  3 //  美团tableVIewCell
  4 //
  5 //  Created by 刘羽 on 15/12/31.
  6 //  Copyright © 2015年 LX. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "Tg.h"
 11 #import "TgCell.h"
 12 #import "TgFooterView.h"
 13 
 14 @interface ViewController () <TgFooterViewDelegate>
 15 @property (nonatomic, strong) NSMutableArray *tgs;
 16 @end
 17 
 18 @implementation ViewController
 19 
 20 - (NSArray *)tgs
 21 {
 22     if (_tgs == nil) _tgs = [Tg tgs];
 23     return _tgs;
 24 }
 25 
 26 - (void)viewDidLoad
 27 {
 28     [super viewDidLoad];
 29     
 30     self.tableView.rowHeight = 80;
 31     
 32     // 调整边距,可以让表格视图让开状态栏
 33     self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
 34     
 35     // footerView
 36     // footerView的宽度会和表格整体宽度一致,只需要指定高度即可
 37     //    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
 38     //    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
 39     //    view.backgroundColor = [UIColor redColor];
 40     //    self.tableView.tableFooterView = view;
 41     // 从XIB加载最后一个视图设置为footerView
 42     TgFooterView *footer = [TgFooterView footerView];
 43     // 视图控制器成为footerView的代理
 44     footer.delegate = self;
 45     self.tableView.tableFooterView = footer;
 46     
 47 //    self.tableView.tableHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"TgHeadView" owner:nil options:nil] lastObject];
 48 }
 49 
 50 ///** 隐藏状态栏 */
 51 //- (BOOL)prefersStatusBarHidden
 52 //{
 53 //    return YES;
 54 //}
 55 
 56 #pragma mark - footerView的代理方法
 57 /**
 58  预处理指令
 59  #if 0
 60  所有代码都不会执行
 61  
 62  #endif
 63  */
 64 - (void)tgFooterViewDidDownloadButtonClick:(TgFooterView *)footerView
 65 {
 66     // 模拟取网络上获取数据加载数据
 67     NSLog(@"努力加载数据中....");
 68     
 69     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 70         // 获得网络数据之后执行的操作
 71         
 72         // 向数组中添加数据,模拟网络加载完成之后的效果
 73         NSDictionary *dict = @{@"title": @"哈哈", @"icon": @"ad_00", @"price": @"100.2", @"buyCount": @"250"};
 74         Tg *tg = [Tg tgWithDict:dict];
 75         
 76         NSLog(@"加数据前 %lu", (unsigned long)self.tgs.count);
 77         
 78         [self.tgs addObject:tg];
 79         
 80         NSLog(@"加数据后 %lu", (unsigned long)self.tgs.count);
 81         // 刷新数据
 82         //    [self.tableView reloadData];
 83         // 新建一个indexPath
 84         NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - 1) inSection:0];
 85         [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
 86         
 87         // 通知页脚视图调整视图显示状态
 88         [footerView endRefresh];
 89     });
 90     
 91 }
 92 
 93 
 94 #pragma mark - 数据源方法
 95 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 96 {
 97     return self.tgs.count;
 98 }
 99 
100 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
101 {
102     // 1. 创建cell
103     TgCell *cell = [TgCell cellWithTableView:tableView];
104     
105     // 2. 通过数据模型,设置Cell内容,可以让视图控制器不需要了解cell内部的实现细节
106     cell.tg = self.tgs[indexPath.row];
107     
108     return cell;
109 }
110 
111 @end

 

美团网页面代码 UITableView

原文:http://www.cnblogs.com/lxzju/p/5092767.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!