NIMCommonTableDelegate.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // NTESCommonTableDelegate.m
  3. // NIM
  4. //
  5. // Created by chris on 15/6/29.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMCommonTableDelegate.h"
  9. #import "NIMCommonTableData.h"
  10. #import "NIMCommonTableViewCell.h"
  11. #import "UIView+NIM.h"
  12. #import "NIMGlobalMacro.h"
  13. #define SepViewTag 10001
  14. static NSString *DefaultTableCell = @"UITableViewCell";
  15. @interface NIMCommonTableDelegate()
  16. @property (nonatomic,copy) NSArray *(^NTESDataReceiver)(void);
  17. @end
  18. @implementation NIMCommonTableDelegate
  19. - (instancetype) initWithTableData:(NSArray *(^)(void))data{
  20. self = [super init];
  21. if (self) {
  22. _NTESDataReceiver = data;
  23. _defaultSeparatorLeftEdge = SepLineLeft;
  24. }
  25. return self;
  26. }
  27. - (NSArray *)data{
  28. return self.NTESDataReceiver();
  29. }
  30. #pragma mark - UITableViewDataSource
  31. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  32. return self.data.count;
  33. }
  34. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  35. NIMCommonTableSection *tableSection = self.data[section];
  36. return tableSection.rows.count;
  37. }
  38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  39. NIMCommonTableSection *tableSection = self.data[indexPath.section];
  40. NIMCommonTableRow *tableRow = tableSection.rows[indexPath.row];
  41. NSString *identity = tableRow.cellClassName.length ? tableRow.cellClassName : DefaultTableCell;
  42. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
  43. if (!cell) {
  44. Class clazz = NSClassFromString(identity);
  45. cell = [[clazz alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identity];
  46. UIView *sep = [[UIView alloc] initWithFrame:CGRectZero];
  47. sep.tag = SepViewTag;
  48. sep.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  49. sep.backgroundColor = [UIColor lightGrayColor];
  50. [cell addSubview:sep];
  51. }
  52. if (![cell respondsToSelector:@selector(refreshData:tableView:)]) {
  53. UITableViewCell *defaultCell = (UITableViewCell *)cell;
  54. [self refreshData:tableRow cell:defaultCell];
  55. }else{
  56. [(id<NIMCommonTableViewCell>)cell refreshData:tableRow tableView:tableView];
  57. }
  58. cell.accessoryType = tableRow.showAccessory ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  59. return cell;
  60. }
  61. #pragma mark - UITableViewDelegate
  62. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  63. NIMCommonTableSection *tableSection = self.data[indexPath.section];
  64. NIMCommonTableRow *tableRow = tableSection.rows[indexPath.row];
  65. return tableRow.uiRowHeight;
  66. }
  67. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  68. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  69. NIMCommonTableSection *tableSection = self.data[indexPath.section];
  70. NIMCommonTableRow *tableRow = tableSection.rows[indexPath.row];
  71. if (!tableRow.forbidSelect) {
  72. UIViewController *vc = tableView.nim_viewController;
  73. NSString *actionName = tableRow.cellActionName;
  74. if (actionName.length) {
  75. SEL sel = NSSelectorFromString(actionName);
  76. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  77. NIMKit_SuppressPerformSelectorLeakWarning([vc performSelector:sel withObject:cell]);
  78. }
  79. }
  80. }
  81. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  82. //这里的cell已经有了正确的bounds
  83. //不在cellForRow的地方设置分割线是因为在ios7下,0.5像素的view利用autoResizeMask调整布局有问题,会导致显示不出来,ios6,ios8均正常。
  84. //具体问题类似http://stackoverflow.com/questions/30663733/add-a-0-5-point-height-subview-to-uinavigationbar-not-show-in-ios7
  85. //这个回调里调整分割线的位置
  86. NIMCommonTableSection *tableSection = self.data[indexPath.section];
  87. NIMCommonTableRow *tableRow = tableSection.rows[indexPath.row];
  88. UIView *sep = [cell viewWithTag:SepViewTag];
  89. CGFloat sepHeight = .5f;
  90. CGFloat sepWidth;
  91. if (tableRow.sepLeftEdge) {
  92. sepWidth = cell.nim_width - tableRow.sepLeftEdge;
  93. }else{
  94. NIMCommonTableSection *section = self.data[indexPath.section];
  95. if (indexPath.row == section.rows.count - 1) {
  96. //最后一行
  97. sepWidth = 0;
  98. }else{
  99. sepWidth = cell.nim_width - self.defaultSeparatorLeftEdge;
  100. }
  101. }
  102. sepWidth = sepWidth > 0 ? sepWidth : 0;
  103. sep.frame = CGRectMake(cell.nim_width - sepWidth, cell.nim_height - sepHeight, sepWidth ,sepHeight);
  104. }
  105. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  106. NIMCommonTableSection *tableSection = self.data[section];
  107. return tableSection.headerTitle;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  110. if (section == 0) {
  111. return 25.f;
  112. }
  113. NIMCommonTableSection *tableSection = self.data[section];
  114. return [tableSection.headerTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.f]}].height;
  115. }
  116. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
  117. NIMCommonTableSection *tableSection = self.data[section];
  118. return tableSection.footerTitle;
  119. }
  120. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  121. NIMCommonTableSection *tableSection = self.data[section];
  122. if (tableSection.headerTitle.length) {
  123. return nil;
  124. }
  125. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  126. return view;
  127. }
  128. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  129. NIMCommonTableSection *tableSection = self.data[section];
  130. if (tableSection.footerTitle.length) {
  131. return nil;
  132. }
  133. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  134. return view;
  135. }
  136. #pragma mark - Private
  137. - (void)refreshData:(NIMCommonTableRow *)rowData cell:(UITableViewCell *)cell{
  138. cell.textLabel.text = rowData.title;
  139. cell.detailTextLabel.text = rowData.detailTitle;
  140. }
  141. @end