提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
这周完成了评论内容,改了一些小bug。收藏界面正在加油,FMDB库目前不是很理解
关于长评论和短评论,我是把他们分成了三个section。第一个section是长评论,第二个section是短评,第三个section是@“已加载全部评论”
第一个section的小标题就是请求到的数据中的长评论的个数,第二个section的小标题就是请求到的数据中短评的个数。
cell的高度我是通过计算对应的文字的行数,然后将行数存到数组里,在返回数组。根据对应的行数来计算对应cell的高度这样子实现
这里是cell上文字高度的计算
labText.numberOfLines = 0;
[labText sizeToFit];
CGFloat height = labText.frame.size.height;
int num = height/labText.font.lineHeight;
NSString* strHeight = [NSString stringWithFormat:@"%d", num];
[self.longHeightArray addObject:strHeight];
labText.numberOfLines = num + 10;
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
labText.attributedText = [[NSAttributedString alloc] initWithString:labText.text attributes:attributes];
这里是cell的各个section和各个高度的返回
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return [self.LongCommentDictionary[@"comments"] count];
} else if (section == 1){
return [self.shortCommentDictionary[@"comments"] count];
} else {
return 1;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// int i = 0;
// int m = 0;
if (indexPath.section == 0 ){
NSString* strHeight = self.longHeightArray[indexPath.row];
int num = [strHeight intValue];
return (num * 20 + 90);
} else if (indexPath.section == 1 && indexPath.row < self.shortHeightArray.count){
NSString* strHeight = self.shortHeightArray[indexPath.row];
int num = [strHeight intValue];
return (num * 20 + 90);
} else{
return 100;
}
return 0;
}
- (void)addView{
[self addSubview:self.commentTableView];
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.contentView.backgroundColor= [UIColor clearColor];
header.textLabel.font = [UIFont boldSystemFontOfSize:20];
header.textLabel.textColor = [UIColor blackColor];
if (section == 0){
NSString* str = [NSString stringWithFormat:@"%lu条长评", [self.LongCommentDictionary[@"comments"] count]];
header.textLabel.text = str;
} else if (section == 1){
NSString* str = [NSString stringWithFormat:@"%lu条短评", [self.shortCommentDictionary[@"comments"] count]];
header.textLabel.text = str;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"111";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 2){
return 0;
} else{
return 20;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 2){
return 0;
} else{
return 20;
}
}
之前写的发现有bug,每次下拉刷新后请求到了前一天的日期,但是每次下拉后改变了所有section的标题,不是一个section的标题
重新使用了一种方法去写这块
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section != 0 && section != self.addSection - 1 && section != 1){
NSString* str = self.allCellArray[section - 2][@"date"];
NSString* strNew = [str substringFromIndex:4];
NSString* dateOne = [strNew substringToIndex:2];
NSString* dateTwo = [strNew substringFromIndex:2];
NSString* date = [NSString stringWithFormat:@" %@月%@日---------------------------", dateOne, dateTwo];
UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 30, 40)];
lab.textColor = [UIColor grayColor];
lab.text = date;
[lab setFont:[UIFont systemFontOfSize:25]];
return lab;
}
return 0;
}
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
if (section != self.addSection - 1){
return @"111";
} else{
return 0;
}
}
发现下拉刷新的bug是下拉三次以上的时候,请求到的两个section的cell就成了相同的。采用大佬的方法,将每次请求到的数据加到一个array里,然后这个cell上的各种信息使用这个array中的数据
- (void)GetBefore{
NSString* dateOld = self.allCellArray[self.addSection - 4][@"date"];
NSDictionary* dic = [NSDictionary dictionaryWithObject:dateOld forKey:@"111"];
NSNotification* notification = [NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic];
[[NSNotificationCenter defaultCenter] postNotification:notification];
//[self.tableView reloadData];
}
- (void)GetBefore{
[[OldManager shareManger]makeData:^(HomeModel * _Nonnull ViewModel) {
self.viewHome.stroiesDictionary = [ViewModel toDictionary];
[self.viewHome.allCellArray addObject:self.viewHome.stroiesDictionary];
dispatch_async(dispatch_get_main_queue(), ^{
[self.viewHome.tableView reloadData];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败!");
}date:(NSString*)self.oldDate];
}
else if (indexPath.section != 0 ){
//在这里进行相应的布局
}
收藏界面FMDB库暂时没看明白。正在加油
这块一开始一直不知道咋写,因为我是使用算出来每个cell上文字的行数然后实现cell的不同的高度。
后来,想到了给每个有回复的cell判断一下回复的行数,如果行数大于2,添加一个“点击展开”的按钮,然后把这个按钮的状态设为0,将按钮的状态存入数组里。然后给这个按钮设置点击事件。每次点击后tableView刷新一次。
然后每次tableView刷新后将存储高度的数组元素清空,在重新存储。然后判断按钮的状态,如果状态为打开且回复行数大于2,评论显示的行数就是应有行数,如果状态关闭且行数大于2,显示行数就为2。如果行数小于2,则没有按钮。
效果如下:
if (num2 >= 2 && [self.shortSelectedArray[indexPath.row] isEqualToString: @"0"]){
NSString* time = self.shortCommentDictionary[@"comments"][indexPath.row][@"time"];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM-dd HH:mm"];
NSDate* confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[time intValue]];
NSString* confroTimespStr = [formatter stringFromDate:confromTimesp];
UILabel* labTime = [[UILabel alloc] init];
labTime.frame = CGRectMake(100, (num+num2)*20 + 50, 100, 30);
labTime.text = confroTimespStr;
[cell.contentView addSubview:labTime];
labTime.textColor = [UIColor grayColor];
labOne.numberOfLines = 2;
UIButton* btnOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnOpen.tag = 100 + indexPath.row;
btnOpen.frame = CGRectMake(110, (num+num2)*20 + 50, WIDTH - 100, 30);
[btnOpen setTitle:@"展开全文" forState:UIControlStateNormal];
[btnOpen setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
btnOpen.titleLabel.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:btnOpen];
[btnOpen addTarget:self action:@selector(pressOpen:) forControlEvents:UIControlEventTouchUpInside];
UIButton* btnLike = [UIButton buttonWithType:UIButtonTypeCustom];
btnLike.frame = CGRectMake(WIDTH - 70, (num+num2) * 20 + 50, 20, 20);
[btnLike setImage:[UIImage imageNamed:@"dianzan-2"] forState:UIControlStateNormal];
[cell.contentView addSubview:btnLike];
UILabel* labLike = [[UILabel alloc] init];
labLike.text = self.shortCommentDictionary[@"comments"][indexPath.row][@"likes"];
labLike.frame = CGRectMake(WIDTH - 90, (num+num2)*20 + 50, 30, 30);
[cell.contentView addSubview:labLike];
UIButton* btnComments = [UIButton buttonWithType:UIButtonTypeCustom];
btnComments.frame = CGRectMake(WIDTH - 40, (num+num2)* 20 + 50, 20, 20);
[btnComments setImage:[UIImage imageNamed:@"shoucang-3"] forState:UIControlStateNormal];
[cell.contentView addSubview:btnComments];
} else if (num2 >= 2 && [self.shortSelectedArray[indexPath.row] isEqualToString: @"1"]){
NSLog(@"YES");
CGFloat height = labOne.frame.size.height;
int num = height/labOne.font.lineHeight;
labOne.numberOfLines = num + 10;
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
labOne.attributedText = [[NSAttributedString alloc] initWithString:labOne.text attributes:attributes];
NSString* time = self.shortCommentDictionary[@"comments"][indexPath.row][@"time"];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM-dd HH:mm"];
NSDate* confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[time intValue]];
NSString* confroTimespStr = [formatter stringFromDate:confromTimesp];
UILabel* labTime = [[UILabel alloc] init];
labTime.frame = CGRectMake(100, (num+num2)*20 + 30, 100, 30);
labTime.text = confroTimespStr;
[cell.contentView addSubview:labTime];
labTime.textColor = [UIColor grayColor];
labOne.numberOfLines = 0;
UIButton* btnLike = [UIButton buttonWithType:UIButtonTypeCustom];
btnLike.frame = CGRectMake(WIDTH - 70, (num+num2) * 20 + 30, 20, 20);
[btnLike setImage:[UIImage imageNamed:@"dianzan-2"] forState:UIControlStateNormal];
[cell.contentView addSubview:btnLike];
UILabel* labLike = [[UILabel alloc] init];
labLike.text = self.shortCommentDictionary[@"comments"][indexPath.row][@"likes"];
labLike.frame = CGRectMake(WIDTH - 90, (num+num2)*20 + 30, 30, 30);
[cell.contentView addSubview:labLike];
UIButton* btnComments = [UIButton buttonWithType:UIButtonTypeCustom];
btnComments.frame = CGRectMake(WIDTH - 40, (num+num2)* 20 + 30, 20, 20);
[btnComments setImage:[UIImage imageNamed:@"shoucang-3"] forState:UIControlStateNormal];
[cell.contentView addSubview:btnComments];
UIButton* btnOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnOpen.tag = 100 + indexPath.row;
btnOpen.frame = CGRectMake(110, (num+num2)*20 +30, WIDTH - 100, 30);
[btnOpen setTitle:@"收起" forState:UIControlStateNormal];
[btnOpen setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
btnOpen.titleLabel.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:btnOpen];
[btnOpen addTarget:self action:@selector(pressOpen:) forControlEvents:UIControlEventTouchUpInside];
x = 0;
}
NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.lineSpacing = 10;
NSMutableDictionary* attributes2 = [NSMutableDictionary dictionary];
[attributes2 setObject:paragraphStyle2 forKey:NSParagraphStyleAttributeName];
labOne.attributedText = [[NSAttributedString alloc] initWithString:labOne.text attributes:attributes2];
NSString* strHeight2 = [NSString stringWithFormat:@"%d", num + num2 + 1];
[self.shortHeightArray addObject:strHeight2];
[cell.contentView addSubview:labOne];
}
收藏界面需要实现在WKWebView上点击收藏或者取消收藏收藏界面都会改变。在收藏的cell上取消收藏收藏界面的收藏状态也会改变。
这里是在首页的WKWebView上点击收藏,然后将这个界面的URL,title,image保存至FMDB库里的函数。
- (void)pressShoucang:(UIButton*)btnShouCang{
btnShouCang.selected = !btnShouCang.selected;
if (btnShouCang.selected == YES){
[btnShouCang setImage:[UIImage imageNamed:@"shoucang-4"] forState:UIControlStateNormal];
BOOL result = [self.collectionDataBase executeUpdate:@"INSERT INTO collectionData (mainLabel, imageUrl, url) VALUES (?, ?, ?);", self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"title"], self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"images"][0], self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"url"]];
if (!result){
NSLog(@"NO");
} else {
NSLog(@"YES");
}
} else {
[btnShouCang setImage:[UIImage imageNamed:@"shoucang-3"] forState:UIControlStateNormal];
if ([self.collectionDataBase open]) {
NSString *sql = @"delete from collectionData where (mainLabel) = (?) and (url) = (?) and (imageUrl) = (?)";
BOOL result = [self.collectionDataBase executeUpdate:sql, self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"title"], self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"url"], self.allCellArray[(btnShouCang.tag - 1000)/6 ][@"stories"][(btnShouCang.tag - 1000) % 6][@"images"][0]];
if (!result) {
NSLog(@"NO");
} else {
}
} else {
NSLog(@"NO");
}
}
NSDictionary* dic = [NSDictionary dictionaryWithObject:self.allCellArray[(btnShouCang.tag % 1000) / 6 ][@"stories"][(btnShouCang.tag % 1000) / 6] forKey:@"111"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"shoucang" object:nil userInfo:dic];
}
这个是在收藏界面里将收藏cell点击取消的代码
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.collectionDataBase open]) {
NSString *sql = @"delete from collectionData where (mainLabel) = (?) and (url) = (?) and (imageUrl) = (?)";
BOOL result = [self.collectionDataBase executeUpdate:sql, self.titleArray[indexPath.row], self.URLArray[indexPath.row], self.imagesArray[indexPath.row]];
if (!result) {
NSLog(@"NO");
} else {
[self.URLArray removeObjectAtIndex:indexPath.row];
[self.titleArray removeObjectAtIndex:indexPath.row];
[self.imagesArray removeObjectAtIndex:indexPath.row];
}
} else {
NSLog(@"NO");
}
[self.collectionDataBase close];
[self.tableView reloadData];
}
这个是在收藏的WKWebView上点击取消收藏的代码
- (void)pressShoucang:(UIButton*)btnShouCang{
btnShouCang.selected = !btnShouCang.selected;
if (btnShouCang.selected == YES){
[btnShouCang setImage:[UIImage imageNamed:@"shoucang-3"] forState:UIControlStateSelected];
if ([self.collectionDataBase open]) {
// 删除数据
NSString *sql = @"delete from collectionData where (mainLabel) = (?) and (url) = (?) and (imageUrl) = (?)";
BOOL result = [self.collectionDataBase executeUpdate:sql, self.titleArray[btnShouCang.tag % 1000], self.URLArray[btnShouCang.tag % 1000], self.imagesArray[btnShouCang.tag % 1000]];
if (!result) {
NSLog(@"数据删除失败");
} else {
[self.URLArray removeObjectAtIndex:btnShouCang.tag % 1000];
[self.titleArray removeObjectAtIndex:btnShouCang.tag % 1000];
[self.imagesArray removeObjectAtIndex:btnShouCang.tag % 1000];
}
} else {
NSLog(@"NO");
}
[self.collectionDataBase close];
[self.tableView reloadData];
} else {
[btnShouCang setImage:[UIImage imageNamed:@"shoucang-4"] forState:UIControlStateSelected];
BOOL result = [self.collectionDataBase executeUpdate:@"INSERT INTO collectionData (mainLabel, imageUrl, url) VALUES (?, ?, ?);", self.titleArray[btnShouCang.tag % 1000], self.imagesArray[btnShouCang.tag % 1000], self.URLArray[btnShouCang.tag % 1000]];
if (!result){
NSLog(@"NO");
} else {
NSLog(@"YES");
}
}
}