一,内容介绍
1. 微博详情界面的实现
2. 评论列表的实现
3.评论内容带上超链接和表情
二,主要技术点
1. 点击单元格Push到下一控制器
2. 评论单元格的自适应
3. 原型单元格的创建
三,主要代码
1. WeiboTableView.m单元格点击事件,在push之前将Weibomodel传入WeiboDetialControllerView中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WeiboModel *weiboModel = _data[indexPath.row];
WeiboDetialViewController *weiboDetialVC = [self.viewController.storyboard instantiateViewControllerWithIdentifier:@"WeiboDetialViewController"];
//找到tableview的控制器,push到下一个控制器
UIViewController *superVC=(UIViewController *)[[tableView superview] nextResponder];
[weiboDetialVC setWeiboModel:weiboModel];
[superVC.navigationController pushViewController:weiboDetialVC animated:YES]; <span style="font-family: FangSong_GB2312;">} </span>
#import "BaseController.h"
@class WeiboModel;
@class CommentTableView;
@interface WeiboDetialViewController : BaseController {
NSMutableArray *_commentData;
}
@property (weak, nonatomic) IBOutlet CommentTableView *TableView;
@property (nonatomic,strong) WeiboModel *weiboModel;
@end
#import "WeiboDetialViewController.h"
#import "MyDataService.h"
#import "WeiboModel.h"
#import "CommentModel.h"
#import "CommentTableView.h"
@interface WeiboDetialViewController ()
@end
@implementation WeiboDetialViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"微博详情";
self.TableView.hidden = YES;
[super showLoading:@"正在加载..."];
_commentData = [[NSMutableArray array] init];
[self _loadCommentdata];
}
- (void) _loadCommentdata {
NSString *weiboID = [self.weiboModel.weiboId stringValue];
NSDictionary *params = @{@"id":weiboID};
[MyDataService requestURl:@"comments/show.json"
httpMethod:@"GET"
params:params
completetion:^(id result) {
NSArray *comments = result[@"comments"];
for (NSDictionary *commentJson in comments) {
CommentModel *commentModel = [[CommentModel alloc] initWithDataDic:commentJson];
[_commentData addObject:commentModel];
}
[_TableView setCommentDatas:_commentData];
[_TableView setWeiboModel:_weiboModel];
[_TableView reloadData];
self.TableView.hidden = NO;
[super showLoading:nil];
}];
}
创建属性,往TableView中传入评论的数据和微博的数据
#import <UIKit/UIKit.h>
@interface WeiboTableView : UITableView <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *data;
@property(nonatomic,strong)NSMutableDictionary *cellHeightCache;
@end
#import "CommentTableView.h"
#import "CommentCell.h"
#import "WeiboCell.h"
@implementation CommentTableView {
NSString *identify;
CommentCell *_proerptCell;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.delegate = self;
self.dataSource = self;
identify = @"CommentCell";
_proerptCell = [self dequeueReusableCellWithIdentifier:identify];
}
return self;
}
#pragma mark - UITableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
}
return _commentDatas.count;
}
//1.创建微博单元格cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
WeiboCell *weiboCell = [[[NSBundle mainBundle] loadNibNamed:@"WeiboCell" owner:nil options:nil] lastObject];
weiboCell.weiboModel = _weiboModel;
return weiboCell;
}
//2.创建评论单元格Cell
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath];
[cell setCmModel:_commentDatas[indexPath.row]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//计算微博单元格的高度
if (indexPath.section == 0) {
WeiboCell *weiboCell = [[[NSBundle mainBundle] loadNibNamed:@"WeiboCell" owner:nil options:nil] lastObject];
weiboCell.width = self.width;
weiboCell.weiboModel = self.weiboModel;
[weiboCell setNeedsLayout];
[weiboCell layoutIfNeeded];
CGSize size = [weiboCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
//计算评论单元格的高度
_proerptCell.cmModel = _commentDatas[indexPath.row];
_proerptCell.width = self.width;
[_proerptCell setNeedsLayout];
[_proerptCell layoutIfNeeded];
CGSize size = [_proerptCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
@end
4. 创建CommentCell,显示数据
#import <UIKit/UIKit.h>
#import "WXLabel.h"
@class WXLabel;
@class CommentModel;
@interface CommentCell : UITableViewCell <WXLabelDelegate> {
__weak IBOutlet UIImageView *_imageView;
__weak IBOutlet UILabel *_nikeLabel;
__weak IBOutlet WXLabel *_commentLabel;
}
@property (nonatomic,strong) CommentModel *cmModel;
@end
#import "CommentCell.h"
#import "CommentModel.h"
#import "UIImageView+WebCache.h"
#import "ThemeManager.h"
@implementation CommentCell
- (void) layoutSubviews {
[super layoutSubviews];
NSString *userImgURL = _cmModel.user.profile_image_url;
[_imageView setImageWithURL:[NSURL URLWithString:userImgURL]];
_nikeLabel.text = _cmModel.user.screen_name;
_commentLabel.text = _cmModel.text;
_commentLabel.wxLabelDelegate = self;
_commentLabel.preferredMaxLayoutWidth = CGRectGetWidth(_commentLabel.bounds);
}
#pragma mark - WXLabel delegate
//检索文本的正则表达式的字符串
- (NSString *)contentsOfRegexStringWithWXLabel:(WXLabel *)wxLabel {
// @用户
NSString *regex1 = @"@[\\w-]+";
// http:// 链接 http(s)://
NSString *regex2 = @"http(s)?://([a-zA-Z0-9.-_]+(/)?)";
// #话题#
NSString *regex3 = @"#.+#";
NSString *regex = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",regex1,regex2,regex3];
return regex;
}
//设置当前链接文本的颜色
- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel {
return [[ThemeManager shareInstance] getThemeColor:@"Link_color"];
}
//设置当前文本手指经过的颜色
- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel {
return [UIColor lightGrayColor];
}
//手指离开当前超链接文本响应的协议方法
- (void)toucheEndWXLabel:(WXLabel *)wxLabel withContext:(NSString *)context {
NSLog(@"%@",context);
}
@end