美文网首页
聊天记录业务逻辑

聊天记录业务逻辑

作者: kangomake | 来源:发表于2021-05-25 14:52 被阅读0次

    从服务器拿来的数据时间是倒叙展示的,即

    list =     (
                    {
                "add_time" = 1621913811;
                "add_time_str" = "11:36:51";
                "from_type" = 1;
                "msg_opt" =             (
                );
                "msg_txt" = "face[\U6cea]face[\U6cea]";
            },
                    {
                "add_time" = 1621913307;
                "add_time_str" = "11:28:27";
                "from_type" = 1;
                "msg_opt" =             (
                );
                "msg_txt" = "face[\U7231\U4f60]face[\U7231\U4f60]";
            },
                    {
                "add_time" = 1621913294;
                "add_time_str" = "11:28:14";
                "from_type" = 1;
                "msg_opt" =             (
                );
                "msg_txt" = "face[\U9119\U89c6]";
            },
    
    

    第一步 首先逆序遍历,即 先是时间小 -》大(距现在最新的时间) :最小的时间展示isTimeVisible = YES;

    if (!self.lastTime ) {
             self.lastTime = time;
            messageModel.isTimeVisible = YES;
     }else{
            long timeInterval = [HRDateTransform compareDate:self.lastTime withDate2:time type:2];
              if(timeInterval >5){//间隔超过5分钟就显示时间 并赋值lastTime
                 messageModel.isTimeVisible = YES;
                 self.lastTime = time;
             }
      }
    
    
    [array addObject:messageModel];
    //遍历到最后一个时间 self.lastTime 置空 nil;
    if(idx == 0){
          self.lastTime = nil;
    }
    

    第二步 把上一步逆序遍历的数据再次逆序遍历(此时数据回到了最初的状态,时间大的在前)

    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           __block HRMessageModel * model = obj;
          [self.dataSource insertObject:model atIndex:0];//每次都插到第一个位置 倒叙展示
     }];
    
    

    注意insertObject:model atIndex:0 这个确保了先把大的放到0位置,然后下一个放到0位置,这样最大的时间(距离现在最近的时间)就被挤到了数组最大的位置,这样tableView展示的时候就把最大时间展示到了最下面的位置

    第三步

    NSInteger count = [mySelf.dataSource count];
    if(page == 1){
         NSInteger pageNum = count >1 ? count-1:0;
         if(count >0){
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:pageNum inSection:0];
                 [mySelf.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
     }else{
    //本页数据总数
           NSInteger pageNum = [array count];
           NSIndexPath *indexPath = [NSIndexPath indexPathForRow:pageNum inSection:0];
            [mySelf.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
     }
    

    解析:
    page == 1时,第一页数据(每页数据20个)
    tableView scrollToRowAtIndexPath:(count-1)即count-1的位置
    page > 1时
    tableView scrollToRowAtIndexPath:(array count)。即第count个的位置
    count是该页数据总数。[array count]

    相关文章

      网友评论

          本文标题:聊天记录业务逻辑

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