Skip to content

Commit 1d9c1fe

Browse files
committed
创建Cell 和 Item 的基类
1 parent 03a6cae commit 1d9c1fe

7 files changed

Lines changed: 367 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ xcuserdata
2323
*.moved-aside
2424
*.xcuserstate
2525
*.xcscmblueprint
26+
.xcworkspacedata
2627

2728
## Obj-C/Swift specific
2829
*.hmap
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// KtBaseTableViewCell.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@class KtTableViewBaseItem;
12+
13+
@interface KtBaseTableViewCell : UITableViewCell
14+
15+
@property (nonatomic, retain) id object;
16+
17+
+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(KtTableViewBaseItem *)object;
18+
19+
@end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// KtBaseTableViewCell.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseTableViewCell.h"
10+
#import "KtTableViewBaseItem.h"
11+
#import "UIView+KtExtension.h"
12+
13+
@implementation KtBaseTableViewCell
14+
15+
- (void)awakeFromNib {
16+
[super awakeFromNib];
17+
// Initialization code
18+
}
19+
20+
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
21+
[super setSelected:selected animated:animated];
22+
23+
// Configure the view for the selected state
24+
}
25+
26+
- (void)setObject:(KtTableViewBaseItem *)object { // 子类在这个方法中解析数据
27+
self.height = [[self class] tableView:nil rowHeightForObject:object];
28+
self.width = [[UIScreen mainScreen] bounds].size.width;
29+
30+
self.contentView.height = self.height;
31+
self.contentView.width = self.width;
32+
33+
self.imageView.image = object.itemImage;
34+
self.textLabel.text = object.itemTitle;
35+
self.detailTextLabel.text = object.itemSubtitle;
36+
self.accessoryView = [[UIImageView alloc] initWithImage:object.itemAccessoryImage];
37+
}
38+
39+
+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(KtTableViewBaseItem *)object {
40+
return 44.0f;
41+
}
42+
43+
@end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// KtTableViewBaseItem.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <UIKit/UIKit.h>
11+
12+
@interface KtTableViewBaseItem : NSObject
13+
14+
@property (nonatomic, retain) NSString *itemIdentifier;
15+
@property (nonatomic, retain) UIImage *itemImage;
16+
@property (nonatomic, retain) NSString *itemTitle;
17+
@property (nonatomic, retain) NSString *itemSubtitle;
18+
@property (nonatomic, retain) UIImage *itemAccessoryImage;
19+
20+
- (instancetype)initWithImage:(UIImage *)image Title:(NSString *)title SubTitle:(NSString *)subTitle AccessoryImage:(UIImage *)accessoryImage;
21+
22+
@end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// KtTableViewBaseItem.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtTableViewBaseItem.h"
10+
11+
@implementation KtTableViewBaseItem
12+
13+
- (instancetype)initWithImage:(UIImage *)image Title:(NSString *)title SubTitle:(NSString *)subTitle AccessoryImage:(UIImage *)accessoryImage {
14+
self = [super init];
15+
if (self) {
16+
_itemImage = image;
17+
_itemTitle = title;
18+
_itemSubtitle = subTitle;
19+
_itemAccessoryImage = accessoryImage;
20+
}
21+
return self;
22+
}
23+
24+
@end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// UIView+KtExtension.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIView (KtExtension)
12+
13+
//frame accessors
14+
/*!
15+
@brief 初始坐标
16+
*/
17+
@property (nonatomic, assign) CGPoint origin;
18+
/*!
19+
@brief View大小
20+
*/
21+
@property (nonatomic, assign) CGSize size;
22+
/*!
23+
@brief 顶部坐标值
24+
*/
25+
@property (nonatomic, assign) CGFloat top;
26+
/*!
27+
@brief 左部坐标值
28+
*/
29+
@property (nonatomic, assign) CGFloat left;
30+
/*!
31+
@brief 底部坐标值
32+
*/
33+
@property (nonatomic, assign) CGFloat bottom;
34+
/*!
35+
@brief 右部坐标值
36+
*/
37+
@property (nonatomic, assign) CGFloat right;
38+
/*!
39+
@brief 宽度值
40+
*/
41+
@property (nonatomic, assign) CGFloat width;
42+
/*!
43+
@brief 高度值
44+
*/
45+
@property (nonatomic, assign) CGFloat height;
46+
/*!
47+
@brief 中心点X坐标
48+
*/
49+
@property (nonatomic, assign) CGFloat x;
50+
/*!
51+
@brief 中心点Y坐标
52+
*/
53+
@property (nonatomic, assign) CGFloat y;
54+
55+
//bounds accessors
56+
/*!
57+
@brief 边界大小
58+
*/
59+
@property (nonatomic, assign) CGSize boundsSize;
60+
/*!
61+
@brief 边界宽度
62+
*/
63+
@property (nonatomic, assign) CGFloat boundsWidth;
64+
/*!
65+
@brief 边界高度
66+
*/
67+
@property (nonatomic, assign) CGFloat boundsHeight;
68+
69+
//content getters
70+
/*!
71+
@brief 边界区域
72+
*/
73+
@property (nonatomic, readonly) CGRect contentBounds;
74+
/*!
75+
@brief 边界中心点
76+
*/
77+
@property (nonatomic, readonly) CGPoint contentCenter;
78+
79+
@end
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//
2+
// UIView+KtExtension.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "UIView+KtExtension.h"
10+
11+
@implementation UIView (KtExtension)
12+
13+
- (CGPoint)origin
14+
{
15+
return self.frame.origin;
16+
}
17+
18+
- (void)setOrigin:(CGPoint)origin
19+
{
20+
CGRect frame = self.frame;
21+
frame.origin = origin;
22+
self.frame = frame;
23+
}
24+
25+
- (CGSize)size
26+
{
27+
return self.frame.size;
28+
}
29+
30+
- (void)setSize:(CGSize)size
31+
{
32+
CGRect frame = self.frame;
33+
frame.size = size;
34+
self.frame = frame;
35+
}
36+
37+
- (CGFloat)top
38+
{
39+
return self.origin.y;
40+
}
41+
42+
- (void)setTop:(CGFloat)top
43+
{
44+
CGRect frame = self.frame;
45+
frame.origin.y = top;
46+
self.frame = frame;
47+
}
48+
49+
- (CGFloat)left
50+
{
51+
return self.origin.x;
52+
}
53+
54+
- (void)setLeft:(CGFloat)left
55+
{
56+
CGRect frame = self.frame;
57+
frame.origin.x = left;
58+
self.frame = frame;
59+
}
60+
61+
- (CGFloat)right
62+
{
63+
return self.left + self.width;
64+
}
65+
66+
- (void)setRight:(CGFloat)right
67+
{
68+
CGRect frame = self.frame;
69+
frame.origin.x = right - frame.size.width;
70+
self.frame = frame;
71+
}
72+
73+
- (CGFloat)bottom
74+
{
75+
return self.top + self.height;
76+
}
77+
78+
- (void)setBottom:(CGFloat)bottom
79+
{
80+
CGRect frame = self.frame;
81+
frame.origin.y = bottom - frame.size.height;
82+
self.frame = frame;
83+
}
84+
85+
- (CGFloat)width
86+
{
87+
return self.size.width;
88+
}
89+
90+
- (void)setWidth:(CGFloat)width
91+
{
92+
CGRect frame = self.frame;
93+
frame.size.width = width;
94+
self.frame = frame;
95+
}
96+
97+
- (CGFloat)height
98+
{
99+
return self.size.height;
100+
}
101+
102+
- (void)setHeight:(CGFloat)height
103+
{
104+
CGRect frame = self.frame;
105+
frame.size.height = height;
106+
self.frame = frame;
107+
}
108+
109+
- (CGFloat)x
110+
{
111+
return self.center.x;
112+
}
113+
114+
- (void)setX:(CGFloat)x
115+
{
116+
self.center = CGPointMake(x, self.center.y);
117+
}
118+
119+
- (CGFloat)y
120+
{
121+
return self.center.y;
122+
}
123+
124+
- (void)setY:(CGFloat)y
125+
{
126+
self.center = CGPointMake(self.center.x, y);
127+
}
128+
129+
//bounds accessors
130+
131+
- (CGSize)boundsSize
132+
{
133+
return self.bounds.size;
134+
}
135+
136+
- (void)setBoundsSize:(CGSize)size
137+
{
138+
CGRect bounds = self.bounds;
139+
bounds.size = size;
140+
self.bounds = bounds;
141+
}
142+
143+
- (CGFloat)boundsWidth
144+
{
145+
return self.boundsSize.width;
146+
}
147+
148+
- (void)setBoundsWidth:(CGFloat)width
149+
{
150+
CGRect bounds = self.bounds;
151+
bounds.size.width = width;
152+
self.bounds = bounds;
153+
}
154+
155+
- (CGFloat)boundsHeight
156+
{
157+
return self.boundsSize.height;
158+
}
159+
160+
- (void)setBoundsHeight:(CGFloat)height
161+
{
162+
CGRect bounds = self.bounds;
163+
bounds.size.height = height;
164+
self.bounds = bounds;
165+
}
166+
167+
//content getters
168+
169+
- (CGRect)contentBounds
170+
{
171+
return CGRectMake(0.0f, 0.0f, self.boundsWidth, self.boundsHeight);
172+
}
173+
174+
- (CGPoint)contentCenter
175+
{
176+
return CGPointMake(self.boundsWidth/2.0f, self.boundsHeight/2.0f);
177+
}
178+
179+
@end

0 commit comments

Comments
 (0)