EMMsgTranspondViewController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // EMMsgTranspondViewController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/20.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMMsgTranspondViewController.h"
  9. #import "EMMessageModel.h"
  10. #import "EMAvatarNameCell.h"
  11. @interface EMMsgTranspondViewController ()
  12. @property (nonatomic, strong) EMMessageModel *model;
  13. @property (nonatomic, strong) NSIndexPath *indexPath;
  14. @end
  15. @implementation EMMsgTranspondViewController
  16. - (instancetype)initWithModel:(EMMessageModel *)aModel
  17. {
  18. self = [super init];
  19. if (self) {
  20. _model = aModel;
  21. }
  22. return self;
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. [self _setupSubviews];
  28. [self loadContactsFromDB];
  29. }
  30. #pragma mark - Subviews
  31. - (void)_setupSubviews
  32. {
  33. [self addPopBackLeftItem];
  34. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"转发" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction)];
  35. self.title = @"转发消息";
  36. self.tableView.rowHeight = 60;
  37. }
  38. #pragma mark - Table view data source
  39. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  40. {
  41. // Return the number of sections.
  42. return 1;
  43. }
  44. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  45. {
  46. // Return the number of rows in the section.
  47. return [self.dataArray count];
  48. }
  49. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  50. {
  51. static NSString *CellIdentifier = @"EMAvatarNameCell";
  52. EMAvatarNameCell *cell = (EMAvatarNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  53. // Configure the cell...
  54. if (cell == nil) {
  55. cell = [[EMAvatarNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  56. UIButton *checkButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 30)];
  57. checkButton.tag = 100;
  58. [checkButton setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
  59. [checkButton setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
  60. checkButton.userInteractionEnabled = NO;
  61. cell.accessoryView = checkButton;
  62. }
  63. cell.avatarView.image = [UIImage imageNamed:@"user_avatar_blue"];
  64. cell.nameLabel.text = [self.dataArray objectAtIndex:indexPath.row];
  65. UIButton *checkButton = (UIButton *)cell.accessoryView;
  66. checkButton.selected = (self.indexPath && self.indexPath.row == indexPath.row) ? YES : NO;
  67. return cell;
  68. }
  69. #pragma mark - Table view delegate
  70. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  73. EMAvatarNameCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  74. UIButton *checkButton = (UIButton *)cell.accessoryView;
  75. if (indexPath.row == self.indexPath.row) {
  76. self.indexPath = nil;
  77. checkButton.selected = NO;
  78. return;
  79. }
  80. EMAvatarNameCell *oldCell = [tableView cellForRowAtIndexPath:self.indexPath];
  81. UIButton *oldButton = (UIButton *)oldCell.accessoryView;
  82. oldButton.selected = NO;
  83. self.indexPath = indexPath;
  84. checkButton.selected = YES;
  85. }
  86. #pragma mark - Data
  87. - (void)loadContactsFromDB
  88. {
  89. NSArray *contacts = [[EMClient sharedClient].contactManager getContacts];
  90. [self.dataArray addObjectsFromArray:contacts];
  91. [self.tableView reloadData];
  92. }
  93. #pragma mark - Action
  94. - (void)doneAction
  95. {
  96. NSString *username = [self.dataArray objectAtIndex:self.indexPath.row];
  97. if (self.doneCompletion) {
  98. self.doneCompletion(self.model, username);
  99. }
  100. [self.navigationController popViewControllerAnimated:YES];
  101. }
  102. @end