WFCUUserMessageListViewController.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // WFCUUserMessageListViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/8/19.
  6. // Copyright © 2020 wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUUserMessageListViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import <WFChatUIKit/WFChatUIKit.h>
  11. @interface WFCUUserMessageListViewController () <UITableViewDelegate, UITableViewDataSource>
  12. @property(nonatomic, strong)UITableView *tableView;
  13. @property(nonatomic, strong)NSMutableArray<WFCCMessage *> *messages;
  14. @property(nonatomic)bool isLoading;
  15. @end
  16. @implementation WFCUUserMessageListViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:NO];
  20. if (userInfo.displayName.length) {
  21. self.title = [NSString stringWithFormat:@"%@ 的消息", userInfo.displayName];
  22. }
  23. self.messages = [[[WFCCIMService sharedWFCIMService] getUserMessages:self.userId conversation:self.conversation contentTypes:nil from:0 count:20] mutableCopy];
  24. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
  25. self.tableView.delegate = self;
  26. self.tableView.dataSource = self;
  27. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  28. [self.view addSubview:self.tableView];
  29. [self.tableView reloadData];
  30. }
  31. - (void)loadMore {
  32. if (!self.messages.count) {
  33. return;
  34. }
  35. NSArray *moreMsg = [[WFCCIMService sharedWFCIMService] getUserMessages:self.userId conversation:self.conversation contentTypes:nil from:[self.messages lastObject].messageId count:20];
  36. [self.messages addObjectsFromArray:moreMsg];
  37. [self.tableView reloadData];
  38. self.isLoading = NO;
  39. }
  40. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  41. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  42. if (!cell) {
  43. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  44. }
  45. WFCCMessage *msg = self.messages[indexPath.row];
  46. cell.textLabel.text = [msg.content digest:msg];
  47. cell.detailTextLabel.text = [WFCUUtilities formatTimeDetailLabel:msg.serverTime];
  48. return cell;
  49. }
  50. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  51. return self.messages.count;
  52. }
  53. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  54. CGPoint offset = scrollView.contentOffset;
  55. CGRect bounds = scrollView.bounds;
  56. CGSize size = scrollView.contentSize;
  57. UIEdgeInsets inset = scrollView.contentInset;
  58. CGFloat scrollViewHeight = bounds.size.height;
  59. CGFloat currentOffset = offset.y + scrollViewHeight - inset.bottom;
  60. CGFloat maximumOffset = size.height;
  61. CGFloat minSpace = 5;
  62. CGFloat maxSpace = 10;
  63. bool isNeedLoadMore = false;
  64. //上拉加载更多
  65. //tableview 的 content的高度 小于 tableview的高度
  66. if(scrollViewHeight < maximumOffset){
  67. CGFloat space = maximumOffset - currentOffset;
  68. if(space < minSpace){
  69. isNeedLoadMore = true;
  70. }
  71. }
  72. if(!self.isLoading && isNeedLoadMore){
  73. self.isLoading = true;
  74. NSLog(@"-->加载更多数据");
  75. [self loadMore];
  76. }
  77. }
  78. @end