NIMTeamMemberListViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // NTESTeamMemberListViewController.m
  3. // NIM
  4. //
  5. // Created by chris on 15/3/26.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMTeamMemberListViewController.h"
  9. #import "NIMTeamCardHeaderCell.h"
  10. #import "NIMCardMemberItem.h"
  11. #import "NIMTeamMemberCardViewController.h"
  12. #define CollectionCellReuseId @"cell"
  13. #define CollectionItemWidth 55
  14. #define CollectionItemHeight 80
  15. #define CollectionEdgeInsetLeftRight 20
  16. #define CollectionEdgeInsetTopFirstLine 25
  17. #define CollectionEdgeInsetTop 15
  18. @interface NIMTeamMemberListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,NIMTeamCardHeaderCellDelegate, NIMTeamMemberCardActionDelegate>
  19. @property (nonatomic,strong) NIMTeam *team;
  20. @property (nonatomic,strong) UICollectionView *collectionView;
  21. @property (nonatomic,copy) NSMutableArray *data;
  22. @property (nonatomic,strong) NIMTeamMember *myTeamCard;
  23. @end
  24. @implementation NIMTeamMemberListViewController
  25. - (instancetype)initTeam:(NIMTeam*)team
  26. members:(NSArray*)members{
  27. self = [super initWithNibName:nil bundle:nil];
  28. if (self) {
  29. _team = team;
  30. _data = [[NSMutableArray alloc] init];
  31. for (NIMTeamMember *member in members) {
  32. NIMTeamCardMemberItem *item = [[NIMTeamCardMemberItem alloc] initWithMember:member];
  33. [_data addObject:item];
  34. if([member.userId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]]) {
  35. _myTeamCard = member;
  36. }
  37. }
  38. }
  39. return self;
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. self.navigationItem.title = @"群成员";
  44. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  45. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
  46. flowLayout.minimumInteritemSpacing = CollectionEdgeInsetLeftRight;
  47. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  48. self.collectionView.backgroundColor = [UIColor colorWithRed:236.0/255.0 green:241.0/255.0 blue:245.0/255.0 alpha:1];
  49. self.collectionView.delegate = self;
  50. self.collectionView.dataSource = self;
  51. [self.collectionView registerClass:[NIMTeamCardHeaderCell class] forCellWithReuseIdentifier:CollectionCellReuseId];
  52. [self.view addSubview:self.collectionView];
  53. self.collectionView.contentInset = UIEdgeInsetsMake(self.collectionView.contentInset.top, CollectionEdgeInsetLeftRight, self.collectionView.contentInset.bottom, CollectionEdgeInsetLeftRight);
  54. }
  55. - (void)viewDidLayoutSubviews{
  56. [super viewDidLayoutSubviews];
  57. }
  58. #pragma mark - UICollectionViewDataSource
  59. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  60. NSInteger lastTotal = self.collectionItemNumber * section;
  61. NSInteger remain = self.data.count - lastTotal;
  62. return remain < self.collectionItemNumber ? remain:self.collectionItemNumber;
  63. }
  64. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  65. NSInteger sections = self.data.count / self.collectionItemNumber;
  66. return self.data.count % self.collectionItemNumber ? sections + 1 : sections;
  67. }
  68. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  69. NIMTeamCardHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseId forIndexPath:indexPath];
  70. cell.delegate = self;
  71. id<NIMKitCardHeaderData> data = [self dataAtIndexPath:indexPath];
  72. [cell refreshData:data];
  73. return cell;
  74. }
  75. - (id<NIMKitCardHeaderData>)dataAtIndexPath:(NSIndexPath*)indexpath{
  76. NSInteger index = indexpath.section * self.collectionItemNumber;
  77. index += indexpath.row;
  78. return self.data[index];
  79. }
  80. - (NSIndexPath *)indexPathForData:(NIMTeamCardMemberItem *)data{
  81. NSInteger index = [self.data indexOfObject:data];
  82. NSInteger section = index / self.collectionItemNumber;
  83. NSInteger row = index % self.collectionItemNumber;
  84. return [NSIndexPath indexPathForRow:row inSection:section];
  85. }
  86. #pragma mark - UICollectionViewDelegateFlowLayout
  87. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  88. return CGSizeMake(CollectionItemWidth, CollectionItemHeight);
  89. }
  90. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  91. if (section == 0) {
  92. return UIEdgeInsetsMake(CollectionEdgeInsetTopFirstLine, 0, 0, 0);
  93. }
  94. return UIEdgeInsetsMake(CollectionEdgeInsetTop, 0, 0, 0);
  95. }
  96. #pragma mark - NIMTeamCardHeaderCellDelegate
  97. - (void)cellDidSelected:(NIMTeamCardHeaderCell*)cell{
  98. NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
  99. NSInteger index = indexpath.section * self.collectionItemNumber;
  100. index += indexpath.row;
  101. NIMTeamMemberCardViewController *vc = [[NIMTeamMemberCardViewController alloc] init];
  102. vc.delegate = self;
  103. NIMTeamCardMemberItem *member = self.data[index];
  104. NIMTeamCardMemberItem *viewer = [[NIMTeamCardMemberItem alloc] initWithMember:self.myTeamCard];
  105. vc.member = member;
  106. vc.viewer = viewer;
  107. [self.navigationController pushViewController:vc animated:YES];
  108. }
  109. #pragma mark - TeamMemberCardActionDelegate
  110. - (void)onTeamMemberKicked:(NIMTeamCardMemberItem *)member {
  111. [self.data removeObject:member];
  112. [_collectionView reloadData];
  113. }
  114. - (void)onTeamMemberInfoChaneged:(NIMTeamCardMemberItem *)member {
  115. [_collectionView reloadData];
  116. }
  117. #pragma mark - 旋转处理 (iOS8 or above)
  118. - (void)viewWillTransitionToSize:(CGSize)size
  119. withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  120. {
  121. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  122. flowLayout.minimumInteritemSpacing = CollectionEdgeInsetLeftRight;
  123. [self.collectionView setCollectionViewLayout:flowLayout animated:YES];
  124. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  125. [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context)
  126. {
  127. [self.collectionView reloadData];
  128. } completion:nil];
  129. }
  130. #pragma mark - Private
  131. - (NSInteger)collectionItemNumber{
  132. CGFloat minSpace = 20.f; //防止计算到最后出现左右贴边的情况
  133. return (int)((self.collectionView.frame.size.width - minSpace)/ (CollectionItemWidth + CollectionEdgeInsetLeftRight));
  134. }
  135. @end