评论界面的实现:
对于评论需要学会自适应cell高度,还要效果一个cell展开改变高的的效果,暂时只写了大致布局
后续:学习了用Masonry约束来自适应cell高度
完善了评论。
刷新页面的bug修改
下拉刷新是要请求三天的文章然后逐个呈现,每次都是一次性网络申请三次,但是上周遗留的问题就出在这里:由于使用for循环申请三次,网络请求所花的时间收服务器,网速等因素影响,所以就会出现这三天的数据出现随机排列的问题,而不是按照for循环的先后次序来添加信息,为此我请教了carry学长来帮我:
使用了先前没接触过的NSLock来限制网络请求的次序
for (int i = 0; i < 3; i++) {
[self.lock lock];
[[Manage shareManage] NetWorkTestWithPreviousData:^(PreviousModel * _Nonnull mainViewModel) {
//
// dispatch_async(dispatch_get_main_queue(), ^{
[self.mainView.allDictionaryArray addObject:[mainViewModel toDictionary]];
self.mainView.cellCount += 1;
[self.mainView.tableView reloadData];
// });
[self.lock unlock];
NSLog(@"请求成功");
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
} JSON:(NSString*) self.stringMutable];
实际只加了两行代码:
[self.lock lock];
[self.lock unlock];
在这两行之间的代码就会被锁定然后优先执行,这样就可以保证线程上不出差错了
其他
学习了FMDB数据库的基本使用
利用一个数组来存储从数据库遍历出来的数据然后按次序布局
//cellViewController内:
- (void) Collect:(NSNotification*) notification {
NSDictionary* dict = notification.userInfo;
NSString* stringURL = dict[@"url"];
NSString* stringSelected = dict[@"selected"];
NSString* stringIndex = dict[@"index"];
if (self.collectionDatabase == nil) {
//1.获得数据库文件的路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@", doc);
NSString *fileName = [doc stringByAppendingPathComponent:@"collectionData.sqlite"];
//2.获得数据库
self.collectionDatabase = [FMDatabase databaseWithPath:fileName];
//3.打开数据库
if ([self.collectionDatabase open]) {
BOOL result = [self.collectionDatabase executeUpdate:@"CREATE TABLE IF NOT EXISTS collectionData (URL text NOT NULL, labelStr text NOT NULL, imageURL text NOT NULL, isSelected text NOT NULL);"];
if (result) {
NSLog(@"创表成功");
} else {
NSLog(@"创表失败");
}
}
}
NSInteger theSection = (self.viewFromCell.pageCountInScrollView - 1) / 6;
NSInteger theRow = (self.viewFromCell.pageCountInScrollView - 1) % 6;
if ([stringSelected isEqualToString: @"YES"]) {
//插入数据的方法
[self insertDataWithURL:stringURL withTitle:self.arrayToStoreDictionary[theSection][@"stories"][theRow][@"title"] withImageURL:self.arrayToStoreDictionary[theSection][@"stories"][theRow][@"images"][0] withSelected:@"YES"];
}
if ([stringSelected isEqualToString: @"NO"]) {
//删除数据的方法
[self deleteDataWithURL:stringURL withTitle:self.arrayToStoreDictionary[theSection][@"stories"][theRow][@"title"] withImageURL:self.arrayToStoreDictionary[theSection][@"stories"][theRow][@"images"] withSelected:@"NO"];
}
}
//插入数据
- (void)insertDataWithURL:(NSString*) url withTitle:(NSString*) stringTitle withImageURL:(NSString*) imageURL withSelected:(NSString*) stringSelected {
if ([self.collectionDatabase open]) {
FMResultSet *resultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];
//判断数据是否已经添加过了
NSInteger numForDetermineToSet = 1;
while ([resultSet next]) {
NSString* URL = [resultSet stringForColumn:@"URL"];
if ([URL isEqualToString:url]) {
numForDetermineToSet = 0;
}
}
if (numForDetermineToSet == 1) {
BOOL result = [self.collectionDatabase executeUpdate:@"INSERT INTO collectionData (URL, labelStr, imageURL, isSelected) VALUES (?, ?, ?, ?);", url, stringTitle, imageURL, stringSelected];
if (!result) {
NSLog(@"增加数据失败");
}else{
NSLog(@"增加数据成功");
}
}
[self.collectionDatabase close];
}
}
//删除数据
- (void) deleteDataWithURL:(NSString*) url withTitle:(NSString*) stringTitle withImageURL:(NSString*) imageURL withSelected:(NSString*) stringSelected {
if ([self.collectionDatabase open]) {
NSString *sql = @"delete from collectionData WHERE URL = ? ";
BOOL result = [self.collectionDatabase executeUpdate:sql, url];
if (!result) {
NSLog(@"数据删除失败");
} else {
NSLog(@"数据删除成功");
}
[self.collectionDatabase close];
}
}
/*--------------------------------------*/
//cellView内:
- (void) collect {
NSString* stringSelected;
if (self.buttonCollect.selected == YES) {
self.buttonCollect.selected = NO;
stringSelected = @"NO";
} else {
self.buttonCollect.selected = YES;
stringSelected = @"YES";
}
NSString* stringIndex = [NSString stringWithFormat:@"%ld", self.pageCountInScrollView - 1];
[[NSNotificationCenter defaultCenter] postNotificationName:@"collectByCellView" object:nil userInfo:@{@"url":self.url, @"selected":stringSelected, @"index":stringIndex}];
}