iOS 實(shí)現(xiàn)優(yōu)酷視頻的評(píng)論回復(fù)功能

一、概述

通過學(xué)習(xí)前面筆者提供的兩種方法來實(shí)現(xiàn)類似微信朋友圈的評(píng)論回復(fù)功能后,首先,筆者來分析兩者兩者的優(yōu)缺點(diǎn),以及兩者的使用場景。其次,筆者將通過方式一即用段頭+Cell+段尾 的方法來實(shí)戰(zhàn)優(yōu)酷視頻的評(píng)論回復(fù)功能,主要分析里面的業(yè)務(wù)邏輯數(shù)據(jù)處理以及細(xì)節(jié)處理。最后,希望能為廣大開發(fā)者提供一點(diǎn)思路,少走一些彎路,填補(bǔ)一些細(xì)坑。

  • 方式一:使用段頭+Cell+段尾推薦使用

    • 優(yōu)點(diǎn):評(píng)論回復(fù)cell(MHCommentCell)發(fā)揮出了UITableViewCell的重用機(jī)制;評(píng)論回復(fù)cell(MHCommentCell)的事件傳遞相比方式二少了一層嵌套;ModelFrame的計(jì)算針對(duì)性強(qiáng)。
    • 缺點(diǎn):要實(shí)現(xiàn)tableView代理的- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section以及- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section方法,以及設(shè)置對(duì)應(yīng)的高度,代碼上略微繁瑣。
    • 使用場景:支持無限評(píng)論回復(fù)的情況下。
  • 方式二:使用UITableViewCell嵌套UITableView

    • 優(yōu)點(diǎn):控制器的代碼相比方式一的簡單。
    • 缺點(diǎn):外層的UITableView發(fā)揮了重用機(jī)制,但是Cell嵌套的UITableView未發(fā)揮重用機(jī)制,如果有1000條評(píng)論回復(fù),那么嵌套的UITableViewCell(MHCommentCell)就得創(chuàng)建1000次,相當(dāng)不合理,性能不好;ModelFrame的計(jì)算稍微復(fù)雜,實(shí)現(xiàn)的計(jì)算好內(nèi)層嵌套的tableView的尺寸;評(píng)論回復(fù)cell(MHCommentCell)的事件傳遞,存在兩層代理嵌套。
    • 使用場景:顯示評(píng)論回復(fù)有限的情況下。
  • 傳送門

二、效果圖
youkuComment.gif
三、頁面分析
  1. 效果圖
MHYouKuControllers.jpg
  1. 圖中字符對(duì)應(yīng)的Controller
    A:MHYouKuController
    B:MHYouKuTopicController (紅色框區(qū)域)
    C:MHYouKuTopicDetailController
    D:MHYouKuCommentController
    注意:文章統(tǒng)一用字符代替對(duì)應(yīng)的Controller,怪我懶,望體諒。

  2. 評(píng)論面板(MHYouKuInputPanelView

MHYouKuInputPanelView@2x.png
四、需求分析
  1. A控制器和B控制器的的評(píng)論數(shù)據(jù)來源問題。
    首先,A控制器和B控制器存在父子關(guān)系,即:[A addChildViewController:B] ,具體使用細(xì)節(jié)請參考文末的Demo鏈接。其次,A控制器里面的tableView無法支持下拉刷新上拉加載功能,B控制器里面的tableView支持的,這種需求在視頻類App非常常見,A控制器只是顯示部分評(píng)論數(shù)據(jù),點(diǎn)擊評(píng)論數(shù)按鈕跳轉(zhuǎn)到B控制器來顯示更多評(píng)論數(shù)據(jù)數(shù)據(jù)。終上所述,筆者采取的是在B控制器里面一旦下拉刷新獲取數(shù)據(jù)后,通過代理或者通知,將評(píng)論數(shù)據(jù)回調(diào)給A控制器處理即可(PS:筆者在此采用的通知:MHCommentRequestDataSuccessNotification)。

    數(shù)據(jù)來源@2x.png

  2. 評(píng)論Cell顯示查看全部xx條回復(fù)的實(shí)現(xiàn)問題。
    筆者這里采取了一種巧妙的方式,配置一個(gè)前端事先制定的MHComment即可。詳細(xì)配置如下

    /** topic --- topicFrame */
    - (MHTopicFrame *)_topicFrameWithTopic:(MHTopic *)topic
    {
        // 這里要判斷評(píng)論個(gè)數(shù)大于2 顯示全部評(píng)論數(shù)
        if (topic.commentsCount>2) {
        // 設(shè)置假數(shù)據(jù)
        MHComment *comment = [[MHComment alloc] init];
        // MHAllCommentsId是前端設(shè)定的
        comment.commentId = MHAllCommentsId;
        comment.text = [NSString stringWithFormat:@"查看全部%zd條回復(fù)" , topic.commentsCount];
        // 添加假數(shù)據(jù)
        [topic.comments addObject:comment];
    }
    
        MHTopicFrame *topicFrame = [[MHTopicFrame alloc] init];
        // 傳遞話題模型數(shù)據(jù),計(jì)算所有子控件的frame
        topicFrame.topic = topic;
        return topicFrame;
    }
    
  3. D控制器對(duì)視頻,評(píng)論成功后,A控制器和B控制器的數(shù)據(jù)同步問題(即:兩者在評(píng)論數(shù)據(jù)上保持一致,從而保證在界面上顯示一致)。
    點(diǎn)擊A控制器和B控制器的評(píng)論框,即可跳轉(zhuǎn)到D控制器,即D控制器是一對(duì)多的關(guān)系。若D控制器對(duì)視頻評(píng)論成功后,筆者通過通知(通知名字:MHCommentSuccessNotification)的方式,來告知AB控制器作相應(yīng)的操作即可。

    視頻評(píng)論@2x.png

  4. 若對(duì)AB以及C控制器里面的某個(gè)視頻評(píng)論進(jìn)行回復(fù)或者針對(duì)某條視頻評(píng)論的回復(fù)進(jìn)行評(píng)論,如何保證A控制器和B控制器的數(shù)據(jù)同步問題(即:兩者在評(píng)論數(shù)據(jù)上保持一致,從而保證在界面上顯示一致)。
    關(guān)鍵:AB以及C控制器持有的是同一個(gè)模型(MHTopicFrame),這樣我們無論對(duì)數(shù)據(jù)做了任何操作,只要監(jiān)聽到改變時(shí),刷新數(shù)據(jù)源方法即可。

    評(píng)論Or回復(fù)@2x.png

  5. 視頻評(píng)論和回復(fù)成功后的數(shù)據(jù)處理問題。
    由于查看全部xx條回復(fù)是前端自己配置,伴隨著視頻的評(píng)論Or回復(fù)成功后,數(shù)據(jù)需要做相應(yīng)的判斷(MHYouKuInputPanelViewDelegate)。

    - (void) inputPanelView:(MHYouKuInputPanelView *)inputPanelView attributedText:(NSString *)attributedText
    {
         // 發(fā)送評(píng)論 模擬網(wǎng)絡(luò)發(fā)送
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.25f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 評(píng)論或者回復(fù)成功
            MHComment *comment = [[MHComment alloc] init];
            comment.mediabase_id = self.mediabase_id;
            comment.commentId = [NSString stringWithFormat:@"%zd",[NSObject mh_randomNumber:0 to:100]];
            comment.text = attributedText;
            comment.creatTime = [NSDate mh_currentTimestamp];
        
            MHUser *fromUser = [[MHUser alloc] init];
            fromUser.userId = [AppDelegate sharedDelegate].account.userId ;
            fromUser.avatarUrl = [AppDelegate sharedDelegate].account.avatarUrl;
            fromUser.nickname = [AppDelegate sharedDelegate].account.nickname;
            comment.fromUser = fromUser;
            // 只有回復(fù)  toUser 有值
            if (inputPanelView.commentReply.isReply) {
                MHUser *toUser = [[MHUser alloc] init];
                toUser.avatarUrl = inputPanelView.commentReply.user.avatarUrl;
                toUser.userId = inputPanelView.commentReply.user.userId;
                toUser.nickname = inputPanelView.commentReply.user.nickname;
                comment.toUser = toUser;
            }
            // 這里需要插入假數(shù)據(jù) 提高用戶的體驗(yàn)度
            MHCommentFrame* newCommentFrame = [[MHTopicManager sharedManager] commentFramesWithComments:@[comment]].lastObject;
            // 這里要插入話題數(shù)據(jù)源中去
            // 修改評(píng)論回復(fù)數(shù)目
            self.selectedTopicFrame.topic.commentsCount  =  self.selectedTopicFrame.topic.commentsCount + 1;
            // 判斷數(shù)據(jù)
            if (self.selectedTopicFrame.topic.comments.count>2) 
                // 有查看全部xx條回復(fù) 
                // 插入數(shù)據(jù)
                NSInteger count = self.selectedTopicFrame.commentFrames.count;
                NSInteger index = count - 1;
                [self.selectedTopicFrame.commentFrames insertObject:newCommentFrame atIndex:index];
                [self.selectedTopicFrame.topic.comments insertObject:comment atIndex:index];
    
                // 取出最后一條數(shù)據(jù) 就是查看全部xx條回復(fù) 修改為xx+1條即可
                MHComment *lastComment = self.selectedTopicFrame.topic.comments.lastObject;
                lastComment.text = [NSString stringWithFormat:@"查看全部%zd條回復(fù)" , self.selectedTopicFrame.topic.commentsCount];
             }else if (self.selectedTopicFrame.topic.comments.count == 2) {
                    // 添加數(shù)據(jù)源
                    [self.selectedTopicFrame.commentFrames addObject:newCommentFrame];
                    [self.selectedTopicFrame.topic.comments addObject:comment];
                
                    // 設(shè)置假數(shù)據(jù)
                    MHComment *lastComment = [[MHComment alloc] init];
                    lastComment.commentId = MHAllCommentsId;
                    lastComment.text = [NSString stringWithFormat:@"查看全部%zd條回復(fù)" , self.selectedTopicFrame.topic.commentsCount];
                    MHCommentFrame *lastCommentFrame =  [[MHTopicManager sharedManager] commentFramesWithComments:@[lastComment]].lastObject;
                    // 添加假數(shù)據(jù)
                    [self.selectedTopicFrame.commentFrames addObject:lastCommentFrame];
                    [self.selectedTopicFrame.topic.comments addObject:lastComment];
                }else{
                    // <2的情況 直接添加即可 
                    // 添加數(shù)據(jù)源
                    [self.selectedTopicFrame.commentFrames addObject:newCommentFrame];
                    [self.selectedTopicFrame.topic.comments addObject:comment];
                }
            // 發(fā)送評(píng)論回復(fù)成功的通知
            [MHNotificationCenter postNotificationName:MHCommentReplySuccessNotification object:nil userInfo:@{MHCommentReplySuccessKey:self.selectedTopicFrame}]; 
        };
    }
    
五、期待
  1. 文章若對(duì)您有點(diǎn)幫助,請給個(gè)喜歡??,畢竟碼字不易;若對(duì)您沒啥幫助,請給點(diǎn)建議??,切記學(xué)無止境。
  2. 針對(duì)文章所述內(nèi)容,閱讀期間任何疑問;請?jiān)谖恼碌撞吭u(píng)論指出,我會(huì)火速解決和修正問題。
  3. GitHub地址:https://github.com/CoderMikeHe
六、代碼

MHDevelopExample_Objective_C - MHYouKuController.h/m

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容