美文网首页自鉴
iOS 评论回复功能(新)

iOS 评论回复功能(新)

作者: anny_4243 | 来源:发表于2016-11-21 14:47 被阅读4179次
    // 点击评论回复按钮
    -(void)reply:(UIButton *)button{
        NSDetailCommentCellModel *model = _dataArray[button.tag];
        [_textFieldView.commentField becomeFirstResponder];
        _textFieldView.model = model;
        _textFieldView.placeHolderLabel.text = [NSString stringWithFormat:@"回复:%@",model.nick_name];
    }
    
    //点击发布按钮
    - (void)publish{
        if (!_textFieldView.model) {
            [self comment];
        }else{
            [self replyAction:_textFieldView.model];
        }
    }
    
    // 评论
    - (void)comment{
        if ([NSString isBlankString:_textFieldView.commentField.text]||[_textFieldView.commentField.text isEqualToString:@"|说说你的看法~"]) {
            [WKProgressHUD popMessage:@"请输入评论文字" inView:self.view duration:HUD_DURATION animated:YES];
        }else{
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            [_textFieldView.commentField resignFirstResponder];
            
            NSDictionary *parameters = @{@"dyStateDesId":self.dyStateDesId,@"add":@"1",@"content":_textFieldView.commentField.text};
            
            [[NSNetworking sharedManager]get:COMMUNITY_DETAIL_COMMENT parameters:parameters success:^(id response) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                [WKProgressHUD popMessage:@"评论成功" inView:self.view duration:HUD_DURATION animated:YES];
                [_textFieldView.commentField resignFirstResponder];
                
                _textFieldView.commentField.textColor = TEXT_COLOR4;
                _textFieldView.commentField.contentInset = UIEdgeInsetsMake(-2,0,2,0);
                _longDetailView.tableview.hidden = NO;
                [_longDetailView.tableview reloadData];
                
                if (self.pageNumber > _dynamicDetailCommentModel.totalPage.integerValue) {
    
                    NSDictionary *dic =@{@"photo":kAccountManager.accountInfo.photo,@"nick_name":kAccountManager.accountInfo.nick_name,@"content":_textFieldView.commentField.text,@"time":@"刚刚"};
                    NSDetailCommentCellModel *model = [[NSDetailCommentCellModel alloc]initWithDictionary:dic error:nil];
                    [_dataArray addObject:model];
                    [self insertTableviewRow];
                    _dynamicDetailCommentModel.totalRow = [NSString stringWithFormat:@"%ld",_dynamicDetailCommentModel.totalRow.integerValue+1];
                    CGSize size = CGSizeMake(screenWidth-63,MAXFLOAT); //设置一个行高上限
                    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:12]};
                    _hotCommentSize = [model.content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
                    [_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.equalTo(@(_longDetailView.tableview.contentSize.height+57+_hotCommentSize.height+64));
                    }];
                    [_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);
                        make.bottom.mas_greaterThanOrEqualTo(_longDetailView);
                    }];
                }else{
                    [_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.equalTo(@(_longDetailView.tableview.contentSize.height+64));
                    }];
                    [_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);
                        make.bottom.mas_greaterThanOrEqualTo(_longDetailView);
                    }];
                }
                _textFieldView.commentField.text = @"|说说你的看法~";
                [self textViewDidChange:_textFieldView.commentField];
                
            } failure:^(NSString *error,int errorCode) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                [WKProgressHUD popMessage:error inView:self.view duration:HUD_DURATION animated:YES];
                _textFieldView.commentField.text = @"|说说你的看法~";
                [self textViewDidChange:_textFieldView.commentField];
                _textFieldView.commentField.textColor = TEXT_COLOR4;
                _textFieldView.commentField.contentInset = UIEdgeInsetsMake(-2,0,2,0);
                
            }];
        }
    }
    
    // 回复
    - (void)replyAction:(NSDetailCommentCellModel *)model{
        NSDictionary *parameters = @{@"dyStateDesId":self.dyStateDesId,@"add":@"1",@"toUser":model.commentUser,@"toComment":model.commentDesId,@"content":_textFieldView.commentField.text};
        
        [[NSNetworking sharedManager]get:COMMUNITY_DETAIL_COMMENT parameters:parameters success:^(id response) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [WKProgressHUD popMessage:@"回复成功" inView:self.view duration:HUD_DURATION animated:YES];
            _longDetailView.tableview.hidden = NO;
            [_longDetailView.tableview reloadData];
            
            if (self.pageNumber > _dynamicDetailCommentModel.totalPage.integerValue) {
                NSDetailCommentCellModel *responseModel = [[NSDetailCommentCellModel alloc]initWithDictionary:response[@"items"][0] error:nil];
                NSDictionary *dic =@{@"photo":kAccountManager.accountInfo.photo,@"nick_name":kAccountManager.accountInfo.nick_name,@"content":_textFieldView.commentField.text,@"time":@"刚刚",@"photoTo":model.photo,@"nick_nameTo":model.nick_name,@"timeTo":model.time,@"contentTo":model.content,@"commentUser":responseModel.commentUser,@"commentDesId":responseModel.commentDesId};
                
                NSDetailCommentCellModel *model = [[NSDetailCommentCellModel alloc]initWithDictionary:dic error:nil];
                [_dataArray addObject:model];
                [self insertTableviewRow];
                _dynamicDetailCommentModel.totalRow = [NSString stringWithFormat:@"%ld",_dynamicDetailCommentModel.totalRow.integerValue+1];
                CGSize size = CGSizeMake(screenWidth-112,MAXFLOAT); //设置一个行高上限
                NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:12]};
                CGSize hotCommentSize = [model.content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
                CGSize size2 = CGSizeMake(screenWidth-110,MAXFLOAT); //设置一个行高上限
                CGSize replySize = [model.contentTo boundingRectWithSize:size2 options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
                [_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(_longDetailView.tableview.contentSize.height+107+hotCommentSize.height+replySize.height+64));
                }];
                [_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);
                    make.bottom.mas_greaterThanOrEqualTo(_longDetailView);
                }];
                _textFieldView.commentField.text = @"|说说你的看法~";
                [self textViewDidChange:_textFieldView.commentField];
                [_textFieldView.commentField resignFirstResponder];
            }else{
                [_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(_longDetailView.tableview.contentSize.height+64));
                }];
                [_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);
                    make.bottom.mas_greaterThanOrEqualTo(_longDetailView);
                }];
            }
            
        } failure:^(NSString *error,int errorCode) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [WKProgressHUD popMessage:error inView:self.view duration:HUD_DURATION animated:YES];
            _textFieldView.commentField.text = @"|说说你的看法~";
            [self textViewDidChange:_textFieldView.commentField];
            _textFieldView.commentField.textColor = TEXT_COLOR4;
            _textFieldView.commentField.contentInset = UIEdgeInsetsMake(-2,0,2,0);
            [_textFieldView.commentField resignFirstResponder];
        }];
    }
    
    // 评论或回复成功后在底部插入一行
    - (void)insertTableviewRow{
        [_longDetailView.tableview beginUpdates];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArray.count-1 inSection:0];
        [_longDetailView.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [_longDetailView.tableview endUpdates];
    }
    
    #pragma mark - UITextViewDelegate
    -(BOOL)textViewShouldEndEditing:(UITextView *)textView{
        _textFieldView.placeHolderLabel.text = @"";
        _textFieldView.model = nil;
        return YES;
    }
    
    评论回复.gif

    相关文章

      网友评论

        本文标题:iOS 评论回复功能(新)

        本文链接:https://www.haomeiwen.com/subject/pfsupttx.html