这篇博文讲的是微博评论列表的页面和如何对微博发评论。

                     

有上面的效果视图可以知道,在微博评论视图中第一个cell是显示微博的内容,接下来的cell才是显示评论内容,评论内容的显示包括:发评论人的昵称,评论内容和评论的时间;而发评论视图就是一个简单的textview和一个评论button,这部分简单。

(1)微博评论视图

①获取微博评论的API是https://api.weibo.com/2/comments/show.json 它的请求的参数除了access_token和微博的id号之外,我还用到了一个参数,就是page,之前已经说过了,数据的返回是以页为单位的,这里一页返回的评论数默认就是50,当然你也可以设置这个参数count来改变每一页返回的评论数,我没有这么做。

所以一共就有三个参数。

+ (NSString *)returnCommentUrlStringWithID:(long long)weiboID page:(int)page {NSString *urlString = [[NSString alloc] initWithFormat:@"%@?access_token=%@&id=%lld&page=%d",COMMENTS,[InfoForSina returnAccessTokenString],weiboID,page];return urlString;
}

其中的COMMENTS表示这个API。

数据的请求和解析如下:

- (void) continueLoadData:(int)page {//GCD异步获取数据dispatch_async(dispatch_get_global_queue(0, 0), ^{dispatch_sync(dispatch_get_global_queue(0, 0), ^{hud.labelText = @"正在加载评论数据...";[hud show:YES];[self.view addSubview:hud];NSString *urlString = [InfoForSina returnCommentUrlStringWithID:_status.statusId page:page];NSURL *url = [NSURL URLWithString:urlString];NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];NSData *commentListData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];NSString *dataListString = [[NSString alloc] initWithData:commentListData encoding:NSUTF8StringEncoding];NSDictionary *dataListDictionary = [dataListString objectFromJSONString];_totalNum = [[dataListDictionary objectForKey:@"total_number"] integerValue];[_commentArray addObjectsFromArray:[dataListDictionary objectForKey:@"comments"]];});dispatch_sync(dispatch_get_main_queue(), ^{[self.tableView reloadData];[hud removeFromSuperview];});});
}

当然,假如评论数超过一页的50条的话,我们继续下来tableview的时候就应该继续加载评论内容。所以我们就要检测是否要继续加载,其中如果是评论总是不超过50和

_page > (_totalNum/50+1)的时候就不用继续数据请求了,给出一个提示框表示数据已经全部加载完毕。

//处理tableview滑动到底了
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {CGPoint contentOffsetPoint = self.tableView.contentOffset;CGRect frame = self.tableView.frame;if (contentOffsetPoint.y == self.tableView.contentSize.height - frame.size.height){if (_totalNum <= 50 ||  _i > (_totalNum/50+1) ) {MBProgressHUD *endHud = [[MBProgressHUD alloc] init];endHud.mode = MBProgressHUDModeText;endHud.labelText = @"提示";endHud.detailsLabelText = @"scroll to the end";[self.tableView addSubview:endHud];[endHud show:YES];[endHud hide:YES afterDelay:1];}else if (_totalNum > [_commentArray count]){[self continueLoadData:++_i];}}
}

②tableview内容的显示

首先第一个cell是显示微博内容,那么我们可以使用segue从微博内容视图传递相应的微博内容数据到这个评论视图,然后在这个视图中再进行显示,显示的规则如微博主页一样。

评论内容的显示我们要注意的是cell高度的计算和cell重用机制可能会导致的内容重叠问题,这个没有什么难度,在前面的博文中我都有介绍过如何解决: iOS UITableViewCell重用问题 和   iOS TableViewCell 动态调整高度。

下面直接上代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{// Configure the cell...if (indexPath.row == 0) {WeiboCell *cell = [[WeiboCell alloc] init];[cell setupCell:_status];//显示微博内容return cell;}else {static NSString *CellIdentifier = @"detailCell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}if (cell != nil) {[cell removeFromSuperview];}//评论数据NSDictionary *dictionary = [_commentArray objectAtIndex:indexPath.row-1];// 获取到一个评论NSString *timeString = [dictionary objectForKey:@"created_at"];//评论时间NSString *commentString = [dictionary objectForKey:@"text"]; //评论内容NSDictionary *user = [dictionary objectForKey:@"user"]; //评论用户NSString *userName = [user objectForKey:@"screen_name"];//用户名//发评论的用户名称UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];[userNameLabel setFont:[UIFont boldSystemFontOfSize:17.0f]];[userNameLabel setText:userName];userNameLabel.adjustsFontSizeToFitWidth = YES;[userNameLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN,CELL_CONTENT_MARGIN, 160,30)];userNameLabel.tag = 100;[[cell viewWithTag:100] removeFromSuperview];[[cell contentView] addSubview:userNameLabel];//设置评论发布时间UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];[timeLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];[timeLabel setText:[self getTimeString:timeString]];[timeLabel setTextAlignment:NSTextAlignmentRight];timeLabel.adjustsFontSizeToFitWidth = YES;[timeLabel setFrame:CGRectMake(170,CELL_CONTENT_MARGIN,140,20)];timeLabel.tag = 101;[[cell viewWithTag:101]removeFromSuperview];[[cell contentView] addSubview:timeLabel];//评论内容UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectZero];[commentLabel setLineBreakMode:NSLineBreakByWordWrapping];[commentLabel setNumberOfLines:0];[commentLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAXFLOAT);CGSize size = [commentString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];[commentLabel setText:commentString];[commentLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, 30+2*CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), size.height)];commentLabel.tag = 102;[[cell viewWithTag:102] removeFromSuperview];[[cell contentView] addSubview:commentLabel];return cell;}
}

注意:

1、tableview cell 的numberOfRowsInSection:返回的应该是 1+评论数,这个1表示这条微博。

2、由于第一个cell 显示的是微博的内容,那么接下来cell的 indexPath.row 和 评论数组中每一条评论的下标就会相差1。也就是说,当indexPath.row = 2时,对于评论数组中评论的下标就是2-1=1。这一点要特别小心,否则会出现数组下标的bug。

这是cell计算高度的代码

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.row == 0) {//返回微博内容cell 的高度,微博内容高度的计算和前面微博主页tableview cell高度的计算一样。 这里为了节省篇幅就不贴代码了。}else {NSDictionary *dictionary = [_commentArray objectAtIndex:indexPath.row-1];// 获取到一个评论  特别要主页这类的数组下标要-1NSString *commentString = [dictionary objectForKey:@"text"]; //评论内容CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAXFLOAT);CGSize size = [commentString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];return 30+3*CELL_CONTENT_MARGIN+size.height;}}

(2)发评论的处理

我们在navigation bar 的右边设置了一个评论按键。使用segue方式连接到发评论视图,这样的好处就是可以传递微博的id号。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {if ([segue.identifier isEqualToString:@"commentSegue"]) {CommentCreatViewController *vc = [segue destinationViewController];vc.weiboID = _status.statusId;}
}

发评论所用到的API是https://api.weibo.com/2/comments/create.json其中需要的参数包括access_token,comment(评论内容必须做URLencode,而且字数一定要在0到140之间)和微博的id。HTTP请求方式为POST。

这里可以用ASIHTTPRequest这个第三方类库来处理,也可以用系统自带的方法处理异步的POST。

下面给出我的代码:

- (IBAction)commentButton:(id)sender {[_commentTextView resignFirstResponder];NSString *content = [[NSString alloc] initWithString:_commentTextView.text];//计算发送微博的内容字数,并作相应的处理NSInteger contentLength = content.length;if (contentLength > 140) {MBProgressHUD *overLengthHud = [[MBProgressHUD alloc] initWithView:self.view];[self.view addSubview:overLengthHud];overLengthHud.mode = MBProgressHUDModeText;overLengthHud.labelText = @"提示信息";overLengthHud.detailsLabelText = [NSString stringWithFormat:@"微博字数:%d 超过140上限!",contentLength];[overLengthHud show:YES];[overLengthHud hide:YES afterDelay:2];}else if(contentLength == 0) {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入评论内容!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];[alert show];}else {_hud = [[MBProgressHUD alloc] init];_hud.dimBackground = YES;_hud.labelText = @"正在发送...";[_hud show:YES];[self.view addSubview:_hud];NSString *accessTokenString = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];NSString *paramString = [NSString stringWithFormat:@"comment=%@&id=%lld&access_token=%@",content,_weiboID,accessTokenString];NSMutableData *postCommentData = [[NSMutableData alloc] init];[postCommentData appendData:[paramString dataUsingEncoding:NSUTF8StringEncoding]];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:COMMENTCREAT] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0];[request setHTTPMethod:@"POST"];[request setHTTPBody:postCommentData];self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];}}#pragma mark - NSURLConnection delegate Methods-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{self.responseData = [[NSMutableData alloc] initWithLength:0];
}-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{[self.responseData appendData:data];
}-(void)connectionDidFinishLoading:(NSURLConnection *)theconnection
{[_hud removeFromSuperview];NSError *error;NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&error];NSString *textString = [dict objectForKey:@"text"];if(textString){MBProgressHUD *successHud = [[MBProgressHUD alloc] init];successHud.mode = MBProgressHUDModeText;successHud.labelText = @"提示";successHud.detailsLabelText = @"发表评论成功!";[successHud show:YES];[self.view addSubview:successHud];[successHud hide:YES afterDelay:1.3];}else {MBProgressHUD *successHud = [[MBProgressHUD alloc] init];successHud.mode = MBProgressHUDModeText;successHud.labelText = @"提示";successHud.detailsLabelText = @"发表评论失败!";[successHud show:YES];[self.view addSubview:successHud];[successHud hide:YES afterDelay:1.3];       }[self.connection cancel];
}-(void)connection:(NSURLConnection *)theconnection didFailWithError:(NSError *)error
{MBProgressHUD *successHud = [[MBProgressHUD alloc] init];successHud.mode = MBProgressHUDModeText;successHud.labelText = @"提示";successHud.detailsLabelText = @"发表评论失败!";[successHud show:YES];[self.view addSubview:successHud];[successHud hide:YES afterDelay:1.3];[self.connection cancel];
}

代码简单,不需要解释了。

好了这一部分的介绍就到此结束了!

iOS 新浪微博客户端Demo实践之(六) 微博评论列表页面和发评论相关推荐

  1. iOS新浪微博客户端开发(4)——自定义微博Cell的实现

    首先看一下效果图(不带转发微博和带转发微博):   一.微博Cell布局分析 微博Cell的详细布局如下图所示,其主要控件有:头像.昵称.微博时间.来源.微博正文.如果有被转发微博,还包括被转发微博的 ...

  2. 伪ios新浪微博客户端(自家用)

    上周老师让我用两周时间来模仿新浪微博的客户端来做一个伪客户端.做工不好,请提出你的宝贵意见.做出来的效果先展示一下. -----------------我是分割线君------------------ ...

  3. Android应用开发-小巫CSDN博客客户端之获取评论列表

    Android应用开发-小巫CSDN博客客户端之获取评论列表 上一篇博客介绍了博文详细内容的业务逻辑实现,本篇博客介绍小巫CSDN博客客户端的最后一项功能,获取评论列表,这个功能的实现跟前面获取文章列 ...

  4. ios小项目——新浪微博客户端总结

    2019独角兽企业重金招聘Python工程师标准>>> 们还是登录不了,你们要用还是得自己申请appkey并且把回调网址设为baidu.或者是再下面留言,留下你的微博uid我把你加入 ...

  5. iOS 新浪微博-5.0 首页微博列表

    首页显示微博列表,是微博的核心部分,这一章节,我们主要是显示出微博的列表. 导入第三方类库 pod 'SDWebImage', '~> 3.7.3' pod 'MJRefresh', '~> ...

  6. Java新浪微博客户端开发第六步(完结开源)

    这次所达到的: 1.修复之前几个严重的Bug 1).查看别的用户的微博时,出现当前登录用户的微博 2).查看粉丝时,出现重复加载的情况. 3).查看微博评论.转发时,出现只有第一条点击的微博可以查看. ...

  7. 新浪微博客户端开发之授权登录+获取微博列表

    新浪微博客户端开发之授权登录+获取微博列表 闲篇: 最近实在是乱得不行,至于怎么乱我也不知该怎么说,那么久没发博客就证明了这点,一般如果小巫有做详尽的计划,并把时间投入到上面的话,我是可以用最短的时间 ...

  8. 微博java客户端开发教程_Java新浪微博客户端开发第四步

    这一步是对之前进行较大的改动.增加的类也比较多.包结构如下: 0.Main:主函数入口 1.MainDialog:主界面 2.WeiboPanel:StatusPanel及CommentPanel的父 ...

  9. java 微博客户端,Java新浪微博客户端开发第二步

    上一篇:Java新浪微博客户端开发***步中有下图,这个access_token就是接下来要用到的. 关于access_token的有效时间: 更多关于access_token与Oauth2,请参看: ...

  10. android 分享到新浪微博客户端,新浪微博新版Android客户端,支持将微博分享给微信好友和微信朋友圈...

    这两天我们都在谈论微信想要"燃烧一切"野心:更新客户端,支持多人语音同时聊天,开放系统API,支持将微信消息同步保存到印象笔记.如今这把火终于烧到了新浪微博,今天新浪微博Andro ...

最新文章

  1. MSDN中关于变体数据类型
  2. 再见SpringMVC!小程序开发工程师岗位职责
  3. 图像化转向名词解释_遥感——数字图像处理名词解释及简单整理
  4. leetcode4:Median of Two Sorted Arrays
  5. mongodb 基础 命令 学习
  6. 《弃子长安》第三章 月下魅影
  7. 数据库设计、查询规范及常用SQL语句
  8. 内核驱动隐藏自身【断链】
  9. 彩色证件照片常用的红色、蓝色背景颜色值 1
  10. 扩展名为bat的文件的创建
  11. RN(React Native)
  12. 上传文件资料并生成缩略图
  13. 2022-2028全球及中国电动直线执行器行业研究及十四五规划分析报告
  14. vue-router 采坑记录
  15. mac自带计算器 进制转换
  16. 几种简单方法找回丢失的IE图标
  17. Ubuntu1804安装
  18. E1视音频编解码器应用方案详细说明
  19. pdf阅读器(福昕pdf阅读器电脑版)免安装pjb
  20. 如何解锁元宇宙?应用场景决定商业化变现

热门文章

  1. 汉字查拼音微信小程序项目源码
  2. JavaScript基础-前端开发
  3. 【学术分享】论文投稿被拒2次,再找第三家的时候突然想放弃怎么办?
  4. 引领智慧教育,联想云桌面如何打造教育“一朵云”?
  5. 入门OJ P:1300 面积题解
  6. java utf8 gbk 乱码,java UTF-8转GBK不乱码
  7. F-Droid换源的坑
  8. DHCP:(3)思科防火墙ASA上部署DHCP服务以及DHCP中继
  9. Python3: chardet 检测 bytes 的原字符串编码格式
  10. 无码间串扰的时域和频域条件