Skip to content

Commit aee598c

Browse files
author
liuhong
committed
添加cell内容映射
1 parent 258260c commit aee598c

File tree

8 files changed

+83
-24
lines changed

8 files changed

+83
-24
lines changed

.DS_Store

0 Bytes
Binary file not shown.

HLSelectView/HLSelectView.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88

99
#import <UIKit/UIKit.h>
1010
@class HLSelectView;
11-
11+
// 监听cell上的按钮点击需要注册此通知
12+
extern NSString *const HLNotificationReadBtnClick;
13+
// 用于设置cell子控件对应的key,不需要外部设置的一定不要设置
14+
extern NSString *const HLCellKeyOfFirstSubview;
15+
extern NSString *const HLCellKeyOfSecondSubview;
16+
extern NSString *const HLCellKeyOfThirdSubview;
17+
extern NSString *const HLCellKeyOfFourSubview;
1218
@protocol HLSelectViewDataSource<NSObject>
1319
@required
14-
- (NSArray *_Nullable)datasWithSelectView:(HLSelectView *)selectView ;
15-
20+
- (NSArray *_Nullable)datasWithSelectView:(HLSelectView *)selectView;
21+
- (UIView *)responseViewWithPoint:(CGPoint)point andEvent:(UIEvent *)event;
22+
//用于设置cell对应子控件的值
23+
- (NSDictionary *)dictionaryWithCellValuesForKeys;
1624
@end
17-
// 监听cell上的按钮点击需要注册此通知
18-
extern NSString *const HLNotificationReadBtnClick;
25+
1926
@interface HLSelectView : UIView
2027

2128
- (instancetype _Nonnull )initWithFrame:(CGRect)frame andTitle:(NSString *_Nullable)title;
2229

23-
@property (nonatomic,strong,nullable) id<HLSelectViewDataSource> dataSource;
30+
@property (nonatomic,weak,nullable) id<HLSelectViewDataSource> dataSource;
2431

2532
@property (nonatomic, assign) CGFloat minY;//浮框允许的最小Y值,即最高处
2633

HLSelectView/HLSelectView.m

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
#define KZoom_Scale_X ((KScreenWidth)/(375.0))
1313
#define KZoom_Scale_Y ((KScreenHeight)/(667.0))
1414
#define EPSILON 1e-6
15+
1516
@interface HLSelectView()<UITableViewDelegate,UITableViewDataSource>
1617
{
1718
struct {
1819
unsigned int datasFlag:1;//是否实现数据源方法
20+
unsigned int responseViewFlag:1;
21+
unsigned int cellKeyValuesFlag:1;
1922
} _delegateFlags;
2023
CGFloat _maxY;//浮框允许的最大y,即最低点
2124
CGRect _frame;
@@ -27,7 +30,10 @@ @interface HLSelectView()<UITableViewDelegate,UITableViewDataSource>
2730
@property (nonatomic, strong) UIView *headerView;
2831
@end
2932

30-
33+
NSString *const HLCellKeyOfFirstSubview = @"firstView";
34+
NSString *const HLCellKeyOfSecondSubview = @"secondView";
35+
NSString *const HLCellKeyOfThirdSubview = @"thirdView";
36+
NSString *const HLCellKeyOfFourSubview = @"fourView";
3137
static CGFloat const KMaxAlpha_ = 0.5;
3238
static CGFloat const KCornerRadiu_ = 12;
3339

@@ -56,9 +62,8 @@ - (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)title
5662

5763
- (void)p_initialSetting
5864
{
59-
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
65+
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
6066
self.minY = _minY ? _minY : self.hlt_height/5.0;
61-
6267
}
6368

6469

@@ -70,7 +75,7 @@ - (void)p_createTableViewWithFrame:(CGRect)frame
7075
containView.layer.shadowOffset = CGSizeMake(0, -6);
7176
containView.layer.shadowOpacity = 1;
7277
containView.layer.shadowRadius = KCornerRadiu_;
73-
78+
7479
containView.layer.backgroundColor = [UIColor whiteColor].CGColor;
7580
UIBezierPath *path = [UIBezierPath bezierPathWithRect:containView.bounds];
7681
containView.layer.shadowPath = path.CGPath;//防止离屏渲染
@@ -86,6 +91,7 @@ - (void)p_createTableViewWithFrame:(CGRect)frame
8691
[self addSubview:containView];
8792
[containView addSubview:tableView];
8893
self.tableView = tableView;
94+
self.tableView.tableFooterView = [UIView new];
8995
self.containView = containView;
9096
}
9197

@@ -214,6 +220,8 @@ -(void)setDataSource:(id<HLSelectViewDataSource>)dataSource
214220
{
215221
_dataSource = dataSource;
216222
_delegateFlags.datasFlag = [self.dataSource respondsToSelector:@selector(datasWithSelectView:)];
223+
_delegateFlags.responseViewFlag = [self.dataSource respondsToSelector:@selector(responseViewWithPoint:andEvent:)];
224+
_delegateFlags.cellKeyValuesFlag = [self.dataSource respondsToSelector:@selector(dictionaryWithCellValuesForKeys)];
217225
}
218226

219227

@@ -237,7 +245,11 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
237245
cell = [[HLTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
238246
cell.selectionStyle = UITableViewCellSelectionStyleNone;
239247
}
240-
cell.dataModel = self.datas[indexPath.row];
248+
NSDictionary *dictionary;
249+
if (_delegateFlags.cellKeyValuesFlag) {
250+
dictionary = [_dataSource dictionaryWithCellValuesForKeys];
251+
}
252+
[cell setCellValueWithDataModel:self.datas[indexPath.row] andKeyValues:dictionary];
241253
return cell;
242254
}
243255

@@ -248,18 +260,30 @@ -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger
248260

249261
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
250262
{
251-
263+
252264
return _headerView;
253265
}
254266

255267
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
256268
{
257269
self.minY = (self.hlt_height - _minY)>tableView.contentSize.height ? self.hlt_height - tableView.contentSize.height : _minY;
258270
}
259-
271+
#pragma mark - system
260272
-(void)dealloc
261273
{
262274
self.headerView = nil;
263275
}
264276

277+
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
278+
{
279+
UIView *responseView;
280+
if (point.y < _containView.hlt_y && _delegateFlags.datasFlag) {
281+
responseView = [_dataSource responseViewWithPoint:point andEvent:event];
282+
responseView = [responseView hitTest:point withEvent:event];
283+
}else{
284+
responseView = [super hitTest:point withEvent:event];
285+
}
286+
return responseView;
287+
}
288+
265289
@end

HLSelectView/HLTableViewCell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ extern NSString *const fontName_;
2626
extern NSString *const HLNotificationReadBtnClick;
2727
@interface HLTableViewCell : UITableViewCell
2828

29-
@property (nonatomic, strong) id dataModel;
29+
- (void)setCellValueWithDataModel:(id)dataModel andKeyValues:(NSDictionary *)kayValues;
3030

3131
@end

HLSelectView/HLTableViewCell.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#import "HLTableViewCell.h"
1010

11+
extern NSString *const HLCellKeyOfFirstSubview;
12+
extern NSString *const HLCellKeyOfSecondSubview;
13+
extern NSString *const HLCellKeyOfThirdSubview;
14+
extern NSString *const HLCellKeyOfFourSubview;
1115
@interface UIView(HLTableViewCellExtension)
1216
- (void)hlt_setOrigin:(CGPoint)origin;
1317
- (CGFloat)hlt_x;
@@ -127,25 +131,24 @@ -(void)p_settingSubviews
127131

128132
}
129133

130-
131134
- (void)readBtnClick:(UIButton *)readBtn
132135
{
133-
134136
[[NSNotificationCenter defaultCenter] postNotificationName:HLNotificationReadBtnClick object:[NSNotification notificationWithName:HLNotificationReadBtnClick object:nil] userInfo:@{@"url":_urlLabel.text}];
135137
}
136138

137-
-(void)setDataModel:(id)dataModel
139+
- (void)setCellValueWithDataModel:(id)dataModel andKeyValues:(NSDictionary *)kayValues
138140
{
139-
_sourceName.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:@"sourceName"] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 10],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
141+
142+
_sourceName.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:kayValues[HLCellKeyOfFirstSubview]] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 10],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
140143
[_sourceName sizeToFit];
141144
_sourceName.hlt_width = _sourceName.hlt_width+10;
142145
_sourceName.layer.cornerRadius = 4;
143146

144147
_selectedPercent.textAlignment = NSTextAlignmentRight;
145-
_selectedPercent.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:@"selectedStr"] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 10],NSForegroundColorAttributeName: [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1.0]}];
148+
_selectedPercent.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:kayValues[HLCellKeyOfThirdSubview]] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 10],NSForegroundColorAttributeName: [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1.0]}];
146149
[_selectedPercent sizeToFit];
147150

148-
_urlLabel.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:@"sourceUrl"] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 14],NSForegroundColorAttributeName: [UIColor colorWithRed:146/255.0 green:154/255.0 blue:163/255.0 alpha:1.0]}];
151+
_urlLabel.attributedText = [[NSAttributedString alloc]initWithString:[dataModel valueForKey:kayValues[HLCellKeyOfSecondSubview]] attributes:@{NSFontAttributeName: [UIFont fontWithName:fontName_ size: 14],NSForegroundColorAttributeName: [UIColor colorWithRed:146/255.0 green:154/255.0 blue:163/255.0 alpha:1.0]}];
149152
[_urlLabel sizeToFit];
150153
}
151154

@@ -156,7 +159,7 @@ -(void)layoutSubviews
156159
//sourceName
157160
point = CGPointMake(15, (self.hlt_height-_sourceName.hlt_height)/2.0);
158161
[_sourceName hlt_setOrigin:point];
159-
162+
160163
//readBtn
161164
point = CGPointMake(self.hlt_width - 15-_readBtn.hlt_width, (self.hlt_height-_readBtn.hlt_height)/2.0);
162165
[_readBtn hlt_setOrigin:point];
@@ -168,7 +171,7 @@ -(void)layoutSubviews
168171
//urlLabel
169172
point = CGPointMake(CGRectGetMaxX(_sourceName.frame)+8, (self.hlt_height-_urlLabel.hlt_height)/2.0);
170173
_urlLabel.frame = (CGRect){.origin=point, .size=CGSizeMake(_selectedPercent.hlt_x-15-point.x, _urlLabel.hlt_height)};
171-
174+
172175
}
173176

174177

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

listView/HLDemoObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
@property (nonatomic, copy, readonly) NSString *sourceName;
1616
@property (nonatomic, copy, readonly) NSString *sourceUrl;
17+
1718
@end

listView/ViewController.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,32 @@ - (void)viewDidLoad {
3535
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readBtnClickNotification:) name:HLNotificationReadBtnClick object:nil];
3636
}
3737

38-
3938
- (void)readBtnClickNotification:(NSNotification *)readBtnClickNotification
4039
{
41-
NSLog(@"%@",readBtnClickNotification.userInfo[@"url"]);
40+
NSLog(@"%@",readBtnClickNotification.userInfo);
4241
}
4342

43+
44+
#pragma mark -- HLSelectViewDataSource
45+
4446
- (NSArray * _Nullable)datasWithSelectView:(HLSelectView *)selectView {
4547
return self.datas;
4648
}
4749

50+
- (UIView *)responseViewWithPoint:(CGPoint)point andEvent:(UIEvent *)event
51+
{
52+
return nil;
53+
}
54+
55+
- (NSDictionary *)dictionaryWithCellValuesForKeys
56+
{
57+
return @{
58+
HLCellKeyOfFirstSubview:@"sourceName",
59+
HLCellKeyOfSecondSubview:@"sourceUrl",
60+
HLCellKeyOfThirdSubview:@"selectedStr"
61+
};
62+
}
63+
4864

4965

5066
@end

0 commit comments

Comments
 (0)