新浪微博(第八天))

     一,内容介绍

  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>


  2. 微博详情的控制器是加载storyboard创建,需要给定Storyboard ID,并添加一个UITableview
#import "BaseController.h"

@class WeiboModel;
@class CommentTableView;
@interface WeiboDetialViewController : BaseController {
    
    NSMutableArray *_commentData;


}
@property (weak, nonatomic) IBOutlet CommentTableView *TableView;

@property (nonatomic,strong) WeiboModel *weiboModel;

@end


请求数据,传入的WeiboModel中带有该条微博的id,考虑到微博详情中会加载Weibomodel中的数据,就一起传了过来

#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];
                     
                
                 
                 }];


}


  3. 自定义CommentTableview 

创建属性,往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


  5. 给评论实现表情,与微博列表中添加表情一致




  

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值