diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.h b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.h index 7705e1b588647793e5a25aefb4e79b0d1f70e54b..121c65b647e13b49f7d5425ea4ae98f01e68562b 100644 --- a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.h +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.h @@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy) NSString *tableName; - (NSArray *)tablesAtDB; +- (NSArray *)dataAtTable; @end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.m index 75a7dde8feed9c8fa6eb76960054463b1c875c00..ae095329b16deaaf189d9cb9283ac45a209552af 100644 --- a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.m +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.m @@ -10,7 +10,7 @@ @interface DoraemonDBManager() - +@property (nonatomic, strong) NSMutableArray *dataArray; @end @@ -56,6 +56,61 @@ return tableNameArray; } +//获取每一张表中的所有数据 +- (NSArray *)dataAtTable{ + sqlite3 *db = [self openDB]; + if (db == nil) { + return nil; + } + //查询sqlite_master表 + NSString *sql = [NSString stringWithFormat:@"select * from %@",self.tableName]; + //执行sql + char *errmsg = nil; + sqlite3_exec(db, [sql UTF8String], selectCallback, nil, &errmsg); + + //处理数据 + NSMutableArray *arrayM = [NSMutableArray arrayWithArray:self.dataArray]; + [self.dataArray removeAllObjects]; + + return arrayM; +} + +//查询回调 +int selectCallback(void *firstValue,int columnCount, char **columnValues, char **columnNames) +{ + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + for (int i = 0; i < columnCount; i++) { + //获取当前的列表(字段名) + char *columnName = columnNames[i]; + NSString *nameStr = nil; + if (columnName == NULL) { + nameStr = nil; + }else{ + nameStr = [NSString stringWithUTF8String:columnName]; + } + + //获取当前字段的值 + char *columnValue = columnValues[i]; + NSString *valueStr = nil; + if (columnValue == NULL) { + valueStr = nil; + }else{ + valueStr = [NSString stringWithUTF8String:columnValue]; + } + + [dict setValue:valueStr forKey:nameStr]; + } + [[[DoraemonDBManager shareManager] dataArray] addObject:dict]; + return 0; +} + +#pragma mark - 懒加载 +- (NSMutableArray *)dataArray{ + if (_dataArray == nil) { + _dataArray = [NSMutableArray array]; + } + return _dataArray; +} @end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.h b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.h new file mode 100644 index 0000000000000000000000000000000000000000..13f3718f4304663447387eef7017ee44d0485e44 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.h @@ -0,0 +1,21 @@ +// +// DoraemonDBCell.h +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import +#import "DoraemonDBRowView.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface DoraemonDBCell : UITableViewCell + +@property (nonatomic, strong) DoraemonDBRowView *rowView; + +- (void)renderCellWithArray:(NSArray *)array; + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.m new file mode 100644 index 0000000000000000000000000000000000000000..80ba4e20301a97c5dc8b677d12fba404d300a6b6 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBCell.m @@ -0,0 +1,35 @@ +// +// DoraemonDBCell.m +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import "DoraemonDBCell.h" +#import "DoraemonDBRowView.h" + +@interface DoraemonDBCell() + +@end + +@implementation DoraemonDBCell + +- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ + self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; + if (self) { + _rowView = [[DoraemonDBRowView alloc] init]; + [self.contentView addSubview:_rowView]; + } + return self; +} + +- (void)layoutSubviews{ + [super layoutSubviews]; + _rowView.frame = self.contentView.bounds; +} + +- (void)renderCellWithArray:(NSArray *)array{ + _rowView.dataArray = array; +} + +@end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.h b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.h new file mode 100644 index 0000000000000000000000000000000000000000..a0b305bba8bed6cf75072824b35b20713e74b204 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.h @@ -0,0 +1,34 @@ +// +// DoraemonDBRowView.h +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import +@class DoraemonDBRowView; + +typedef NS_ENUM(NSInteger, DoraemonDBRowViewType) { + DoraemonDBRowViewTypeForTitle = 0, + DoraemonDBRowViewTypeForOne = 1, + DoraemonDBRowViewTypeForTwo = 2 + +}; + +@protocol DoraemonDBRowViewTypeDelegate + +- (void)rowView:(DoraemonDBRowView *)rowView didLabelTaped:(UILabel *)label; + +@end + + +@interface DoraemonDBRowView : UIView + +@property(nonatomic, copy) NSArray *dataArray; + +@property(nonatomic, assign) DoraemonDBRowViewType type; + +@property(nonatomic, weak) id delegate; + +@end + diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.m new file mode 100644 index 0000000000000000000000000000000000000000..7ffb1cee386241534a9659adb2003203682f97a2 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBRowView.m @@ -0,0 +1,63 @@ +// +// DoraemonDBRowView.m +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import "DoraemonDBRowView.h" +#import "DoraemonDefine.h" + +@implementation DoraemonDBRowView + +- (instancetype)initWithFrame:(CGRect)frame{ + if (self = [super initWithFrame:frame]) { + + } + return self; +} + +- (void)setDataArray:(NSArray *)dataArray{ + _dataArray = dataArray; + for (UIView *sub in self.subviews) { + [sub removeFromSuperview]; + } + for (int i = 0; i < self.dataArray.count; i++) { + NSString *content = self.dataArray[i]; + UILabel *label = [[UILabel alloc] init]; + UIColor *color = [UIColor doraemon_colorWithString:@"#dcdcdc"]; + if (self.type == DoraemonDBRowViewTypeForOne) { + color = [UIColor doraemon_colorWithString:@"#e6e6e6"]; + } + if (self.type == DoraemonDBRowViewTypeForTwo) { + color = [UIColor doraemon_colorWithString:@"#ebebeb"]; + } + label.backgroundColor = color; + label.text = content; + label.textAlignment = NSTextAlignmentCenter; + label.tag = i; + label.userInteractionEnabled = YES; + [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel:)]]; + [self addSubview:label]; + } +} + +- (void)layoutSubviews{ + [super layoutSubviews]; + + for (UIView *subView in self.subviews) { + if ([subView isKindOfClass:UILabel.class]) { + CGFloat width = (self.bounds.size.width - (self.dataArray.count - 1)) / self.dataArray.count; + subView.frame = CGRectMake(subView.tag * (width + 1), 0, width, self.bounds.size.height); + } + } +} + +- (void)tapLabel:(UITapGestureRecognizer *)tap{ + UILabel *label = (UILabel *)tap.view; + if ([self.delegate respondsToSelector:@selector(rowView:didLabelTaped:)]) { + [self.delegate rowView:self didLabelTaped:label]; + } +} + +@end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.h b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.h new file mode 100644 index 0000000000000000000000000000000000000000..2caf4d57c660db046e0cdf19ca528b24efa01da6 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.h @@ -0,0 +1,18 @@ +// +// DoraemonDBShowView.h +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface DoraemonDBShowView : UIView + +- (void)showText:(NSString *)text; + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.m new file mode 100644 index 0000000000000000000000000000000000000000..398aaf757f2643994c8a931f9f8db2aa2f20b9ef --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBShowView.m @@ -0,0 +1,43 @@ +// +// DoraemonDBShowView.m +// AFNetworking +// +// Created by yixiang on 2019/4/1. +// + +#import "DoraemonDBShowView.h" +#import "DoraemonDefine.h" + +@interface DoraemonDBShowView() + +@property (nonatomic, strong) UILabel *displayLabel; + +@end + +@implementation DoraemonDBShowView + +- (instancetype)initWithFrame:(CGRect)frame{ + self = [super initWithFrame:frame]; + if (self) { + _displayLabel = [[UILabel alloc] init]; + _displayLabel.numberOfLines = 0; + _displayLabel.textAlignment = NSTextAlignmentCenter; + _displayLabel.backgroundColor = [UIColor doraemon_colorWithString:@"#AAAAAA"]; + [self addSubview:_displayLabel]; + } + return self; +} + +- (void)showText:(NSString *)text{ + _displayLabel.frame = CGRectMake(self.doraemon_width/2-150/2, self.doraemon_height/2-100/2, 150, 100); + __weak typeof(self) weakSelf = self; + [UIView animateWithDuration:0.25 animations:^{ + __strong __typeof(weakSelf) strongSelf = weakSelf; + strongSelf.displayLabel.frame = CGRectMake(self.doraemon_width/2-300/2, self.doraemon_height/2-200/2, 300, 200); + } completion:^(BOOL finished) { + __strong __typeof(weakSelf) strongSelf = weakSelf; + strongSelf.displayLabel.text = text; + }]; +} + +@end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DoraemonDBTableViewController.h b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBTableViewController.h similarity index 100% rename from iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DoraemonDBTableViewController.h rename to iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBTableViewController.h diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBTableViewController.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBTableViewController.m new file mode 100644 index 0000000000000000000000000000000000000000..018b76955478b7e26bb05f6b7b33e13cdac6d903 --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DB/DoraemonDBTableViewController.m @@ -0,0 +1,129 @@ +// +// DoraemonDBTableViewController.m +// AFNetworking +// +// Created by yixiang on 2019/3/31. +// + +#import "DoraemonDBTableViewController.h" +#import "DoraemonDBManager.h" +#import "DoraemonDBRowView.h" +#import "DoraemonDBCell.h" +#import "DoraemonDBShowView.h" + +@interface DoraemonDBTableViewController () + +@property (nonatomic, strong) UIScrollView *scrollView; +@property (nonatomic, strong) UITableView *tableView; +@property (nonatomic, copy) NSArray *dataAtTable; +@property (nonatomic, strong) DoraemonDBShowView *showView; + + +@end + +@implementation DoraemonDBTableViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + self.title = [DoraemonDBManager shareManager].tableName; + + NSArray *dataAtTable = [[DoraemonDBManager shareManager] dataAtTable]; + self.dataAtTable = dataAtTable; + + UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; + scrollView.backgroundColor = [UIColor whiteColor]; + scrollView.bounces = NO; + [self.view addSubview:scrollView]; + self.scrollView = scrollView; + + UITableView *tableView = [[UITableView alloc] init]; + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; + tableView.backgroundColor = [UIColor whiteColor]; + tableView.delegate = self; + tableView.dataSource = self; + [self.scrollView addSubview:tableView]; + self.tableView = tableView; + +} + +- (void)viewWillLayoutSubviews{ + [super viewWillLayoutSubviews]; + + if (self.dataAtTable.count) { + NSDictionary *dict = self.dataAtTable.firstObject; + NSUInteger num = [dict allKeys].count; + self.tableView.frame = CGRectMake(0, 0, num * 200, self.scrollView.frame.size.height); + self.scrollView.contentSize = CGSizeMake(self.tableView.frame.size.width, self.tableView.bounds.size.height); + } +} + +#pragma mark - UITableViewDelegate,UITableViewDataSource +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ + return self.dataAtTable.count == 0 ? 0 : 1; +} + +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ + DoraemonDBRowView *headerView = nil; + if (headerView == nil) { + headerView = [[DoraemonDBRowView alloc] init]; + } + + NSDictionary *dict = self.dataAtTable.firstObject; + headerView.dataArray = [dict allKeys]; + + return headerView; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ + return 44; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ + return self.dataAtTable.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ + static NSString *identifer = @"db_data"; + DoraemonDBCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer]; + if (cell == nil) { + cell = [[DoraemonDBCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer]; + cell.selectionStyle = UITableViewCellSelectionStyleNone; + cell.rowView.delegate = self; + } + + cell.rowView.type = (indexPath.row % 2 == 0) ? DoraemonDBRowViewTypeForOne : DoraemonDBRowViewTypeForTwo; + NSDictionary *dict = self.dataAtTable[indexPath.row]; + [cell renderCellWithArray:[dict allValues]]; + + return cell; +} + +#pragma mark -- DoraemonDBRowViewTypeDelegate +- (void)rowView:(DoraemonDBRowView *)rowView didLabelTaped:(UILabel *)label{ + NSString *content = label.text; + NSLog(@"%@",content); + [self showText:content]; +} + +#pragma mark -- 显示弹出文案 +- (void)showText:(NSString *)content{ + if (self.showView) { + [self.showView removeFromSuperview]; + } + self.showView = [[DoraemonDBShowView alloc] initWithFrame:self.view.bounds]; + [self.view addSubview:self.showView]; + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]; + self.showView.userInteractionEnabled = YES; + [self.showView addGestureRecognizer:tap]; + + [self.showView showText:content]; +} + +- (void)dismissView{ + if (self.showView) { + [self.showView removeFromSuperview]; + } +} + + +@end diff --git a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DoraemonDBTableViewController.m b/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DoraemonDBTableViewController.m deleted file mode 100644 index 529deba01ef4e273044f52eb5f71bd9a883585b4..0000000000000000000000000000000000000000 --- a/iOS/DoraemonKit/Src/Core/Plugin/Sanbox/VC/DoraemonDBTableViewController.m +++ /dev/null @@ -1,24 +0,0 @@ -// -// DoraemonDBTableViewController.m -// AFNetworking -// -// Created by yixiang on 2019/3/31. -// - -#import "DoraemonDBTableViewController.h" -#import "DoraemonDBManager.h" - -@interface DoraemonDBTableViewController () - -@end - -@implementation DoraemonDBTableViewController - -- (void)viewDidLoad { - [super viewDidLoad]; - self.title = [DoraemonDBManager shareManager].tableName; - - -} - -@end