WFCUSeletedUserViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //
  2. // SeletedUserViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by Zack Zhang on 2020/4/2.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSeletedUserViewController.h"
  9. #import "WFCUSelectedUserCollectionViewCell.h"
  10. #import "WFCUSelectedUserTableViewCell.h"
  11. #import "WFCUUserSectionKeySupport.h"
  12. #import "UIFont+YH.h"
  13. #import "UIColor+YH.h"
  14. #import "UIImage+ERCategory.h"
  15. #import "WFCUConfigManager.h"
  16. #import "WFCUSeletedUserSearchResultViewController.h"
  17. #import "UIView+Toast.h"
  18. #define SearchBarMinWidth 70
  19. //#import "WFCCIMService.h"
  20. @interface WFCUSeletedUserViewController ()
  21. <UITableViewDataSource, UITableViewDelegate,
  22. UICollectionViewDataSource, UICollectionViewDelegate,
  23. UISearchBarDelegate>
  24. @property (nonatomic, strong)UITableView *tableView;
  25. @property (nonatomic, strong)UIView *topView;
  26. @property (nonatomic, strong)UICollectionView *selectedUserCollectionView;
  27. @property (nonatomic, strong)UISearchBar *searchBar;
  28. @property (nonatomic, strong)UIButton *doneButton;
  29. @property (nonatomic, strong)NSMutableArray<WFCUSelectedUserInfo *> *dataSource;
  30. @property (nonatomic, strong)NSDictionary *sectionDictionary;
  31. @property (nonatomic, strong)NSArray *sectionKeys;
  32. @property(nonatomic, assign)BOOL sorting;
  33. @property(nonatomic, assign)BOOL needSort;
  34. @property (nonatomic, strong)NSMutableArray<WFCUSelectedUserInfo *> *selectedUsers;
  35. @end
  36. @implementation WFCUSeletedUserViewController
  37. #pragma mark - Life Circle
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. self.selectedUsers = [[NSMutableArray alloc] init];
  41. for (NSString *defaultUserId in self.disableUserIds) {
  42. WFCUSelectedUserInfo *defaultUser = [[WFCUSelectedUserInfo alloc] init];
  43. defaultUser.selectedStatus = Disable;
  44. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:defaultUserId inGroup:self.groupId refresh:NO];
  45. defaultUser.userId = defaultUserId;
  46. defaultUser.displayName = userInfo.displayName;
  47. defaultUser.groupAlias = userInfo.groupAlias;
  48. defaultUser.friendAlias = userInfo.friendAlias;
  49. defaultUser.portrait = userInfo.portrait;
  50. [self.selectedUsers addObject:defaultUser];
  51. }
  52. [self loadData];
  53. [self setUpUI];
  54. }
  55. - (void)viewDidLayoutSubviews {
  56. [super viewDidLayoutSubviews];
  57. [self resizeAllView];
  58. }
  59. - (void)viewWillAppear:(BOOL)animated {
  60. [super viewWillAppear:animated];
  61. }
  62. - (void)viewWillDisappear:(BOOL)animated {
  63. [super viewWillDisappear:animated];
  64. }
  65. #pragma mark - kvo
  66. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  67. if ([keyPath isEqualToString:@"contentSize"]) {
  68. [self resizeAllView];
  69. }
  70. }
  71. #pragma mark - UISearchBarDelegate
  72. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  73. WFCUSeletedUserSearchResultViewController *resultVC = [[WFCUSeletedUserSearchResultViewController alloc] init];
  74. __weak typeof(self)weakSelf = self;
  75. resultVC.dataSource = self.dataSource;
  76. resultVC.needSection = self.type == Horizontal;
  77. resultVC.selectedUser = ^(WFCUSelectedUserInfo * _Nonnull user) {
  78. [weakSelf toggelSeletedUser:user];
  79. };
  80. UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:resultVC];
  81. naviVC.modalPresentationStyle = UIModalPresentationFullScreen;
  82. [self presentViewController:naviVC animated:NO completion:nil];
  83. return NO;
  84. }
  85. #pragma mark - UITableViewDataSource
  86. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  87. if (self.type == Horizontal) {
  88. return self.sectionKeys.count;
  89. } else {
  90. return 1;
  91. }
  92. }
  93. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  94. if (self.type == Horizontal) {
  95. NSString *key = self.sectionKeys[section];
  96. NSArray *users = self.sectionDictionary[key];
  97. return users.count;
  98. } else {
  99. return self.dataSource.count;
  100. }
  101. }
  102. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  103. WFCUSelectedUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectedUserT"];
  104. if (self.type == Horizontal) {
  105. NSString *key = self.sectionKeys[indexPath.section];
  106. NSArray *users = self.sectionDictionary[key];
  107. cell.selectedUserInfo = users[indexPath.row];
  108. } else {
  109. cell.selectedUserInfo = self.dataSource[indexPath.row];
  110. }
  111. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  112. if (self.type == Vertical) {
  113. cell.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  114. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  115. cell.nameLabel.textColor = [UIColor whiteColor];
  116. cell.nameLabel.textColor = [UIColor whiteColor];
  117. } else {
  118. cell.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
  119. cell.backgroundColor = [UIColor whiteColor];
  120. cell.nameLabel.textColor = [UIColor colorWithHexString:@"0x1d1d1d"];
  121. }
  122. return cell;
  123. }
  124. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  125. if (self.type == Horizontal) {
  126. NSString *title = self.sectionKeys[section];
  127. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
  128. view.backgroundColor = [UIColor colorWithHexString:@"0xededed"];
  129. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, self.view.frame.size.width, 30)];
  130. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  131. label.textColor = [UIColor colorWithHexString:@"0x828282"];
  132. label.textAlignment = NSTextAlignmentLeft;
  133. label.text = [NSString stringWithFormat:@"%@", title];
  134. [view addSubview:label];
  135. return view;
  136. } else {
  137. return nil;
  138. }
  139. }
  140. - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  141. if (self.type == Horizontal) {
  142. return self.sectionKeys;
  143. } else {
  144. return nil;
  145. }
  146. }
  147. #pragma mark - UITableViewDelegate
  148. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  149. return 51;
  150. }
  151. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  152. if (self.type == Horizontal) {
  153. if (section == 0) {
  154. return 0;
  155. }
  156. return 30;
  157. } else {
  158. return 0;
  159. }
  160. }
  161. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  162. if (self.type == Vertical) {
  163. WFCUSelectedUserInfo *user = nil;
  164. user = self.dataSource[indexPath.row];
  165. [self toggelSeletedUser:user];
  166. } else {
  167. NSString *key = self.sectionKeys[indexPath.section];
  168. NSArray *users = self.sectionDictionary[key];
  169. WFCUSelectedUserInfo *user = nil;
  170. user = users[indexPath.row];
  171. [self toggelSeletedUser:user];
  172. }
  173. }
  174. #pragma mark - UICollectionViewDataSource
  175. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  176. WFCUSelectedUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"selectedUserC" forIndexPath:indexPath];
  177. cell.user = self.selectedUsers[indexPath.row];
  178. cell.isSmall = self.type == Horizontal;
  179. return cell;
  180. }
  181. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  182. return self.selectedUsers.count;
  183. }
  184. #pragma mark - UICollectionViewDelegate
  185. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  186. [self toggelSeletedUser:self.selectedUsers[indexPath.row]];
  187. }
  188. #pragma mark - private
  189. - (void)resizeAllView {
  190. CGFloat topSpace = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  191. if (self.type == Vertical) {
  192. CGFloat collectionViewHeight = 0;
  193. CGSize contentSize = self.selectedUserCollectionView.contentSize;
  194. if (contentSize.height > 52 * 2 + 10) {
  195. collectionViewHeight = 52 * 2 + 10;
  196. } else {
  197. collectionViewHeight = contentSize.height;
  198. }
  199. self.selectedUserCollectionView.frame = CGRectMake(16, 0, self.view.frame.size.width - 16 * 2, collectionViewHeight);
  200. self.searchBar.frame = CGRectMake(16, collectionViewHeight + 12, self.view.frame.size.width - 16 * 2, 38);
  201. self.topView.frame = CGRectMake(0, topSpace, self.view.frame.size.width, collectionViewHeight + 12 + 26 + 16);
  202. self.tableView.frame = CGRectMake(0, topSpace + collectionViewHeight + 12 + 26 + 16, self.view.frame.size.width, self.view.frame.size.height - (collectionViewHeight + 12 + 26 + 16 + topSpace));
  203. } else {
  204. CGFloat collectionViewWidth = 0;
  205. CGFloat collectionMaxWidth = self.view.frame.size.width - (16 + SearchBarMinWidth + 8 * 2);
  206. CGSize contentSize = self.selectedUserCollectionView.contentSize;
  207. if (contentSize.width > collectionMaxWidth) {
  208. collectionViewWidth = collectionMaxWidth;
  209. } else {
  210. collectionViewWidth = contentSize.width;
  211. }
  212. self.selectedUserCollectionView.frame = CGRectMake(16, 6, collectionViewWidth, 40);
  213. self.searchBar.frame = CGRectMake(16 + collectionViewWidth + 8, 0, self.view.frame.size.width - (16 + collectionViewWidth + 8 * 2), 52);
  214. self.topView.frame = CGRectMake(0, topSpace, self.view.frame.size.width, 60);
  215. self.tableView.frame = CGRectMake(0, topSpace + 60, self.view.frame.size.width, self.view.frame.size.height - (60 + topSpace + 2));
  216. }
  217. }
  218. - (void)loadData {
  219. self.dataSource = [NSMutableArray new];
  220. NSArray *userDataSource = nil;
  221. if (self.inputData) {
  222. userDataSource = self.inputData;
  223. } else if (self.candidateUsers) {
  224. userDataSource = [[WFCCIMService sharedWFCIMService] getUserInfos:self.candidateUsers inGroup:nil];
  225. } else {
  226. NSArray *userIdList = [[WFCCIMService sharedWFCIMService] getMyFriendList:YES];
  227. userDataSource = [[WFCCIMService sharedWFCIMService] getUserInfos:userIdList inGroup:nil];
  228. }
  229. for (WFCCUserInfo *userInfo in userDataSource) {
  230. WFCUSelectedUserInfo *info = [[WFCUSelectedUserInfo alloc] init];
  231. [info cloneFrom:userInfo];
  232. if ([self.disableUserIds containsObject:info.userId]) {
  233. info.selectedStatus = Disable;
  234. }
  235. [self.dataSource addObject:info];
  236. }
  237. [self sortAndRefreshWithList:self.dataSource];
  238. }
  239. - (void)setUpUI {
  240. if (self.type != No) {
  241. [self.view addSubview:self.topView];
  242. [self.topView addSubview:self.searchBar];
  243. [self.topView addSubview:self.selectedUserCollectionView];
  244. }
  245. [self.view addSubview:self.tableView];
  246. if (self.type == Vertical) {
  247. self.view.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  248. self.tableView.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  249. self.searchBar.barTintColor = [UIColor colorWithHexString:@"313236"];
  250. self.selectedUserCollectionView.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  251. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor colorWithHexString:@"313236"] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  252. [self.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  253. self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0x1f2026"];
  254. UINavigationBar *bar = [UINavigationBar appearance];
  255. bar.barTintColor = [UIColor colorWithHexString:@"0x1f2026"];
  256. bar.tintColor = [UIColor whiteColor];
  257. bar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
  258. bar.barStyle = UIBarStyleDefault;
  259. if (@available(iOS 13, *)) {
  260. UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc] init];
  261. bar.standardAppearance = navBarAppearance;
  262. bar.scrollEdgeAppearance = navBarAppearance;
  263. navBarAppearance.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  264. navBarAppearance.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
  265. }
  266. self.title = @"选择成员";
  267. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
  268. self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  269. self.doneButton.frame = CGRectMake(0, 0, 52, 30);
  270. [self setDoneButtonStyleAndContent:NO];
  271. self.doneButton.backgroundColor = [UIColor colorWithHexString:@"0x3e65e4"];
  272. [self.doneButton setTitle:@"完成" forState:UIControlStateNormal];
  273. self.doneButton.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:15];
  274. [self.doneButton setTintColor:[UIColor whiteColor]];
  275. self.doneButton.layer.cornerRadius = 4;
  276. self.doneButton.layer.masksToBounds = YES;
  277. self.doneButton.enabled = NO;
  278. [self.doneButton addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
  279. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneButton];
  280. } else {
  281. self.view.backgroundColor = [UIColor whiteColor];
  282. self.tableView.backgroundColor = [UIColor whiteColor];
  283. self.selectedUserCollectionView.backgroundColor = [UIColor whiteColor];
  284. self.searchBar.barTintColor = [UIColor whiteColor];
  285. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  286. [self.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  287. self.title = @"创建会话";
  288. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
  289. self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  290. self.doneButton.frame = CGRectMake(0, 0, 52, 30);
  291. [self setDoneButtonStyleAndContent:NO];
  292. self.doneButton.backgroundColor = [UIColor colorWithHexString:@"0x3e65e4"];
  293. [self.doneButton setTitle:@"完成" forState:UIControlStateNormal];
  294. self.doneButton.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:15];
  295. [self.doneButton setTintColor:[UIColor whiteColor]];
  296. self.doneButton.layer.cornerRadius = 4;
  297. self.doneButton.layer.masksToBounds = YES;
  298. self.doneButton.enabled = NO;
  299. [self.doneButton addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
  300. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneButton];
  301. }
  302. }
  303. - (void)setDoneButtonStyleAndContent:(BOOL)enable {
  304. if (enable) {
  305. self.doneButton.enabled = YES;
  306. self.doneButton.alpha = 1.0;
  307. if (self.type == Horizontal) {
  308. [self.doneButton setTitle:[NSString stringWithFormat:@"完成(%lu)", (unsigned long)self.selectedUsers.count] forState:UIControlStateNormal];
  309. [self.doneButton sizeToFit];
  310. self.doneButton.frame = CGRectMake(0, 0, self.doneButton.frame.size.width + 8 * 2, self.doneButton.frame.size.height);
  311. } else {
  312. [self.doneButton setTitle:[NSString stringWithFormat:@"完成(%lu/%d)", (unsigned long)self.selectedUsers.count, self.maxSelectCount] forState:UIControlStateNormal];
  313. [self.doneButton sizeToFit];
  314. self.doneButton.frame = CGRectMake(0, 0, self.doneButton.frame.size.width + 8 * 2, self.doneButton.frame.size.height);
  315. }
  316. } else {
  317. self.doneButton.enabled = NO;
  318. self.doneButton.alpha = 0.6;
  319. self.doneButton.frame = CGRectMake(0, 0, 52, 30);
  320. [self.doneButton setTitle:@"完成" forState:UIControlStateNormal];
  321. }
  322. }
  323. - (void)cancel {
  324. [_selectedUserCollectionView removeObserver:self forKeyPath:@"contentSize"];
  325. [[WFCUConfigManager globalManager] setupNavBar];
  326. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  327. }
  328. - (void)finish {
  329. [_selectedUserCollectionView removeObserver:self forKeyPath:@"contentSize"];
  330. [[WFCUConfigManager globalManager] setupNavBar];
  331. NSMutableArray *selectedUserIds = [NSMutableArray new];
  332. for (WFCUSelectedUserInfo *user in self.selectedUsers) {
  333. if (user.selectedStatus == Checked) {
  334. [selectedUserIds addObject:user.userId];
  335. }
  336. }
  337. self.selectResult(selectedUserIds);
  338. [self dismissViewControllerAnimated:NO completion:nil];
  339. }
  340. - (void)sortAndRefreshWithList:(NSArray *)friendList {
  341. // self.sorting = YES;
  342. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  343. NSMutableDictionary *resultDic = [WFCUUserSectionKeySupport userSectionKeys:friendList];
  344. dispatch_async(dispatch_get_main_queue(), ^{
  345. self.sectionDictionary = resultDic[@"infoDic"];
  346. self.sectionKeys = resultDic[@"allKeys"];
  347. [self.tableView reloadData];
  348. });
  349. });
  350. }
  351. - (BOOL)toggelSeletedUser:(WFCUSelectedUserInfo *)user {
  352. if (user.selectedStatus == Disable) {
  353. return NO;
  354. } else if (user.selectedStatus == Checked) {
  355. user.selectedStatus = Unchecked;
  356. NSIndexPath *removeIndexPath = [NSIndexPath indexPathForItem:[self.selectedUsers indexOfObject:user] inSection:0];
  357. [self.selectedUsers removeObject:user];
  358. [self.selectedUserCollectionView deleteItemsAtIndexPaths:@[removeIndexPath]];
  359. } else if (user.selectedStatus == Unchecked) {
  360. if (self.maxSelectCount > 0 && self.selectedUsers.count >= self.maxSelectCount) {
  361. [self.view makeToast:WFCString(@"MaxCount")];
  362. return NO;
  363. }
  364. user.selectedStatus = Checked;
  365. [self.selectedUsers addObject:user];
  366. NSIndexPath *insertIndexPath = [NSIndexPath indexPathForItem:self.selectedUsers.count - 1 inSection:0];
  367. [self.selectedUserCollectionView insertItemsAtIndexPaths:@[insertIndexPath]];
  368. __weak typeof(self)weakSelf = self;
  369. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  370. if (weakSelf.type == Vertical) {
  371. [weakSelf.selectedUserCollectionView scrollToItemAtIndexPath:insertIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
  372. } else {
  373. [weakSelf.selectedUserCollectionView scrollToItemAtIndexPath:insertIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
  374. }
  375. });
  376. }
  377. [self setDoneButtonStyleAndContent:self.selectedUsers.count > 0];
  378. NSIndexPath *indexPath = nil;
  379. if (self.type == Vertical) {
  380. indexPath = [NSIndexPath indexPathForRow:[self.dataSource indexOfObject:user] inSection:0];
  381. } else {
  382. indexPath = [self getSectionIndexPath:user];
  383. }
  384. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  385. return YES;
  386. }
  387. - (NSIndexPath *)getSectionIndexPath:(WFCUSelectedUserInfo *)user {
  388. NSIndexPath *indexPath = nil;
  389. for (NSString *key in self.sectionKeys) {
  390. NSArray *users = self.sectionDictionary[key];
  391. for (WFCUSelectedUserInfo *u in users) {
  392. if ([u isEqual:user]) {
  393. NSInteger section = [self.sectionKeys indexOfObject:key];
  394. NSInteger row = [users indexOfObject:u];
  395. indexPath = [NSIndexPath indexPathForRow:row inSection:section];
  396. }
  397. }
  398. }
  399. return indexPath;
  400. }
  401. #pragma mark - lazy load
  402. - (UICollectionView *)selectedUserCollectionView {
  403. if (!_selectedUserCollectionView) {
  404. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  405. CGRect rect = CGRectZero;
  406. if (self.type == Vertical) {
  407. flowLayout.itemSize = CGSizeMake(52, 52);
  408. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  409. rect = CGRectMake(16, 0, self.view.frame.size.width - 16 * 2, 1);
  410. } else {
  411. flowLayout.itemSize = CGSizeMake(40, 40);
  412. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  413. rect = CGRectMake(16, 6, 1, 24);
  414. }
  415. _selectedUserCollectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flowLayout];
  416. _selectedUserCollectionView.delegate = self;
  417. _selectedUserCollectionView.dataSource = self;
  418. [_selectedUserCollectionView registerClass:[WFCUSelectedUserCollectionViewCell class] forCellWithReuseIdentifier:@"selectedUserC"];
  419. [_selectedUserCollectionView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
  420. }
  421. return _selectedUserCollectionView;
  422. }
  423. - (UITableView *)tableView {
  424. if (!_tableView) {
  425. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  426. _tableView.delegate = self;
  427. _tableView.dataSource = self;
  428. // _tableView.frame = self.view.bounds;
  429. _tableView.sectionIndexColor = [UIColor colorWithHexString:@"0x4e4e4e"];
  430. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  431. [_tableView registerClass:[WFCUSelectedUserTableViewCell class] forCellReuseIdentifier:@"selectedUserT"];
  432. }
  433. return _tableView;
  434. }
  435. - (UIView *)topView {
  436. if (!_topView) {
  437. _topView = [UIView new];
  438. if (self.type == Horizontal) {
  439. _topView.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  440. UIView *insertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 52)];
  441. insertView.backgroundColor = [UIColor whiteColor];
  442. [_topView addSubview:insertView];
  443. }
  444. }
  445. return _topView;
  446. }
  447. - (UISearchBar *)searchBar {
  448. if (!_searchBar) {
  449. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
  450. _searchBar.delegate = self;
  451. _searchBar.placeholder = @"搜索";
  452. _searchBar.barStyle = UIBarStyleDefault;
  453. }
  454. return _searchBar;
  455. }
  456. @end