自定义微博步骤:
1.观察应用,分析功能,了解答题流程
2.加载plist取出数据,同时建立模型储存到数组中,因为这是个自定义cell,每个cell的高度都是由cell里面内容确定的,但是要设置cell的高度就要的hi用代理的的这个方法:
-
(CGFloat)tableVIew:(UITableView *) heightForRowAtIndexPath:(NSIndexPath
*)IndexPath
问题来了,我们要设置cell的内容就需要在创建cell的方法里将对象赋值过去,但是设置高度的方法是在创建cell方法之前调用的,这个时候我们还不知道cell的内容。怎么办?
其实我们写到后面就发现了,计算cell高度只需要plist里取出的数据就可以了,我们要加里一个Frame模型,用来记录各个控件的宽高,计算出cell的高度,在这个Frame模型里包含一个weibo的基础模型之后,我么就可以在之后的传值的时候传递这个Frame模型就可以了
/**
* 懒加载数据模型
*/
-
(NSArray *)weibos
{
// 懒加载一定要判断防止重复加载
if (!_weibos)
{
NSString *path
=
[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *array
=
[NSArray arrayWithContentsOfFile:path];
// 数据转模型
NSMutableArray *modle
=
[NSMutableArray arrayWithCapacity:array.count];
for (NSDictionary *dict in array)
{
LYweiboFrame *weiboFrame
=
[[LYweiboFrame alloc] init];
weiboFrame.weibo =
[LYWeibo weiboWithDictionaty:dict];
[modle addObject:weiboFrame];
}
self.weibos =
[modle copy];
}
return _weibos;
}
3.创建weibocellView
在这个cell初始化的时候,将需要的控件加上去,这里有需要注意的
自定义cellVIew里面使用的初始化方法是:init方法
如果是Xib创建的cell,那么初始化方法是:
initWithCoder:和:
awakeFromNib
这一点是需要注意的
4.现在什么都有了,就剩下填充数据了。在CellVIew里面写一个方法,将传过去模型的数据,填充到建立的各个控件里去就可以了。
上代码:
LYViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 |
// // LYViewController.m // TG小项目 // // Created by liye on 14-5-28. // Copyright (c) 2014年 heima. All rights reserved. // #import "LYViewController.h" #import "LYTgModel.h" #import "LYCellView.h" #import "LYFooterView.h" #import "LYHeadderView.h" @interface
LYViewController ()<UITableViewDataSource, LYFooterViewDelegate> /** * - tableView */ @property
(weak, nonatomic ) IBOutlet
UITableView *tableView; /** * - 懒加载数组 */ @property
(strong, nonatomic ) NSMutableArray
*tgs; @end @implementation
LYViewController #pragma mark 代理方法 - ( void )footViewLoadMoreBtn:(LYFooterView *)footerView { LYTgModel *newTg = [[LYTgModel alloc] init]; newTg.title = @ "一家人快餐" ; newTg.price = @ "¥9.9" ; newTg.buyCount = @ "0" ; // 添加数据 [ self .tgs addObject:newTg]; // 刷新数据 [ self .tableView reloadData]; } #pragma mark viewDidLoad - ( void )viewDidLoad { [ super
viewDidLoad]; // 设置底部视图 LYFooterView *footerView = [LYFooterView footerView]; // 设置底部视图代理 footerView.delegate = self ; self .tableView.tableFooterView = footerView; // 设置顶部视图 LYHeadderView *headderView = [LYHeadderView headderView]; self .tableView.tableHeaderView = headderView; } #pragma mark -tableView /** * tableView返回多少组 */ - ( NSInteger )numberOfSectionsInTableView:(UITableView *)tableView { return
1; } /** * 每组多少行 */ - ( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section { return
self .tgs.count; } /** * 每行的数据 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath
*)indexPath { // 创建cell LYCellView *cell = [LYCellView addCell:tableView]; // 设置cell cell.tg = self .tgs[indexPath.row]; // 返回cell return
cell; } #pragma mark 去掉状态栏 - ( BOOL )prefersStatusBarHidden { return
YES ; } #pragma mark -懒加载数据 - ( NSArray
*)tgs { // 懒加载一定要判断防止重复加载 if
(!_tgs) { NSString
*path = [[ NSBundle
mainBundle] pathForResource:@ "tgs.plist"
ofType: nil ]; NSArray
*array = [ NSArray
arrayWithContentsOfFile:path]; // 数据转模型 NSMutableArray
*modle = [ NSMutableArray
arrayWithCapacity:array.count]; for
( NSDictionary
*dict in array) { [modle addObject:[LYTgModel tgWithDictionaty:dict]]; } self .tgs = [modle mutableCopy]; } return
_tgs; } @end |
创建模型(先引入一个宏)
LYGolbal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
// // LYGolbal.h // UITable练习 // // Created by liye on 14-5-27. // Copyright (c) 2014年 heima. All rights reserved. // #ifndef UITable___LYGolbal_h #define UITable___LYGolbal_h #define LYinitH(name) \ - (instancetype)initHeroWithDictionary:( NSDictionary
*)dict;\ + (instancetype)name##WithDictionaty:( NSDictionary
*)dict; #define LYinitM(name)\ - (instancetype)initHeroWithDictionary:( NSDictionary
*)dict\ {\ if ( self = [ super
init]) {\ [ self
setValuesForKeysWithDictionary:dict];\ }\ return
self ;\ }\ + (instancetype)name##WithDictionaty:( NSDictionary
*)dict\ {\ return
[[ self alloc] initHeroWithDictionary:dict];\ } #endif |
建立模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
#import <Foundation/Foundation.h> #import "LYGolbal.h" @interface
LYTgModel : NSObject @property
( nonatomic , copy ) NSString
*title; @property
( nonatomic , copy ) NSString
*buyCount; @property
( nonatomic , copy ) NSString
*icon; @property
( nonatomic , copy ) NSString
*price; LYinitH(tg); @end #import "LYTgModel.h" #import "LYGolbal.h" @implementation
LYTgModel LYinitM(tg); @end |
CellView:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
#import <UIKit/UIKit.h> @class LYTgModel; @interface
LYCellView : UITableViewCell @property
(strong, nonatomic ) LYTgModel *tg; + (instancetype)addCell:(UITableView *)tableView; @end #import "LYCellView.h" #import "LYTgModel.h" @interface
LYCellView () @property
(weak, nonatomic ) IBOutlet
UILabel *titleLabel; @property
(weak, nonatomic ) IBOutlet
UILabel *priceLabel; @property
(weak, nonatomic ) IBOutlet
UILabel *buyCount; @property
(weak, nonatomic ) IBOutlet
UIImageView *cellImageView; @end @implementation
LYCellView + (instancetype)addCell:(UITableView *)tableView { // cell标记 NSString
*cellMark = @ "tg" ; // 从缓存中取出cell LYCellView *cell = [tableView dequeueReusableCellWithIdentifier:cellMark]; // 判断是否存在cell if
(!cell) { cell = [[ NSBundle
mainBundle] loadNibNamed:@ "LYCellView"
owner: nil
options: nil ][0]; } return
cell; } - ( void )setTg:(LYTgModel *)tg { if
( self ) { self .cellImageView.image = [UIImage imageNamed:tg.icon]; self .titleLabel.text = tg.title; self .priceLabel.text = [ NSString
stringWithFormat:@ "¥%@" , tg.price]; self .buyCount.text = [ NSString
stringWithFormat:@ "%@已购买" , tg.buyCount]; } } @end |
靠,xib文件怎么搞
算了,你们自己建把,这里需要一个LYCellView.xib
LYheadderView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 |
#import <UIKit/UIKit.h> @interface
LYHeadderView : UIView <UIScrollViewDelegate> + (LYHeadderView *)headderView; @end #define kImageCount 5 #define kWidth 300 #define kHeight 140 #import "LYHeadderView.h" @interface
LYHeadderView () @property
(weak, nonatomic ) IBOutlet
UIScrollView *headScrollView; @property
(weak, nonatomic ) IBOutlet
UIPageControl *pageNumber; @property
(strong, nonatomic ) NSTimer
*timer; @end @implementation
LYHeadderView /** * 添加视图headderView */ + (LYHeadderView *)headderView { // 从Bundle中取出 NSArray
*array = [[ NSBundle
mainBundle] loadNibNamed:@ "LYHeadderView"
owner: nil
options: nil ]; return
array[0]; } /** * 为视图添加数据 */ - ( void )awakeFromNib { for
( int i = 0; i < kImageCount; i ++) { UIImageView *headderImageView = [[UIImageView alloc] init]; headderImageView.frame = CGRectMake(i * kWidth, 0, kWidth, kHeight); NSString
*imageName = [ NSString
stringWithFormat:@ "ad_%02d" , i]; headderImageView.image = [UIImage imageNamed:imageName]; [ self .headScrollView addSubview:headderImageView]; } // 滚动效果 self .headScrollView.contentSize = CGSizeMake(kImageCount * kWidth, kHeight); // 取消垂直方向的滚动条 self .headScrollView.showsVerticalScrollIndicator = NO ; // 半自动分页 self .headScrollView.pagingEnabled = YES ; // 设置代理 self .headScrollView.delegate = self ; // 自动播放 [ self
startTimer]; } #pragma mark 代理方法 - ( void )nextImage { if
( self .pageNumber.currentPage == kImageCount - 1) { self .pageNumber.currentPage = 0; } else { self .pageNumber.currentPage ++; } CGFloat currentX = kWidth * ( self .pageNumber.currentPage); [ self .headScrollView setContentOffset:CGPointMake(currentX, 0) animated: YES ]; } // 只要图片滚动,就会计算图片当前页数 - ( void )scrollViewDidScroll:(UIScrollView *)scrollView { if
(! self .timer) { // 设置图片页数 CGFloat currentX = self .headScrollView.contentOffset.x; self .pageNumber.currentPage = (currentX + kWidth * 0.5) / kWidth; } } // 拖动时,关闭计时器 - ( void )scrollViewWillBeginDragging:(UIScrollView *)scrollView { [ self .timer invalidate]; self .timer = nil ; } // 结束拖动,开始计时器 - ( void )scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:( BOOL )decelerate { [ self
startTimer]; } // 开启计时器方法 - ( void )startTimer { self .timer = [ NSTimer
timerWithTimeInterval:1.f target: self
selector: @selector (nextImage) userInfo: nil
repeats: YES ]; [[ NSRunLoop
mainRunLoop] addTimer: self .timer forMode: NSRunLoopCommonModes ]; } @end |
这里需要一个LYHeadderView.xib
LYFooterView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 |
#import <UIKit/UIKit.h> @class LYFooterView; @protocol
LYFooterViewDelegate < NSObject > @optional - ( void )footViewLoadMoreBtn:(LYFooterView *)footerView; @end @interface
LYFooterView : UIView @property
(strong, nonatomic ) id <LYFooterViewDelegate> delegate; + (instancetype)footerView; @end #import "LYFooterView.h" @interface
LYFooterView() @property
(weak, nonatomic ) IBOutlet
UIButton *loadMoreBtn; @property
(weak, nonatomic ) IBOutlet
UIView *hiddenView; @end @implementation
LYFooterView + (instancetype)footerView { return
[[ NSBundle
mainBundle] loadNibNamed:@ "LYFooterView"
owner: nil
options: nil ][0]; } /** * 按钮点击事件 */ - ( IBAction )loadMoreBtnClick { NSLog (@ "加一个" ); // HiddenView显示,Button隐藏 self .hiddenView.hidden = NO ; self .loadMoreBtn.hidden = YES ; // 代理添加一条数据 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC )), dispatch_get_main_queue(), ^{ // 使用代理 if
([ self .delegate respondsToSelector: @selector (footViewLoadMoreBtn:)]) { [ self .delegate footViewLoadMoreBtn: self ]; } // HiddenView隐藏,Button显示 self .hiddenView.hidden = YES ; self .loadMoreBtn.hidden = NO ; }); } @end |
小叶子是小白,只是在这里回顾一下流程,巩固知识
tableView用法----博客状态案例,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/daliye-blog/p/3760238.html