SLMomentsVc.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // SLMomentsVc.m
  3. // SLAiELTS
  4. //
  5. // Created by Gusont on 2023/4/14.
  6. //
  7. #import "SLMomentsVc.h"
  8. #import "SDTimeLineTableHeaderView.h"
  9. #import "SDTimeLineCell.h"
  10. #import "SDTimeLineCellModel.h"
  11. #import "UITableView+SDAutoTableViewCellHeight.h"
  12. #import "UIView+SDAutoLayout.h"
  13. #import "SLMoentsChatView.h"
  14. #import "SLMomentsCommentVc.h"
  15. #import "SLMomentDetailTableVc.h"
  16. #import "SLCreatMomentsVc.h"
  17. #define kTimeLineTableViewCellId @"SDTimeLineCell"
  18. static CGFloat textFieldH = 56;
  19. @interface SLMomentsVc () <SDTimeLineCellDelegate, UITextFieldDelegate, Delegates>
  20. @property (nonatomic, strong) UITextView *textField;
  21. @property (nonatomic, assign) BOOL isReplayingComment;
  22. @property (nonatomic, strong) NSIndexPath *currentEditingIndexthPath;
  23. @property (nonatomic, copy) NSString *commentToUser;
  24. @property (nonatomic, strong) SLMoentsChatView *moentsChatView;
  25. @property (nonatomic, strong) SDTimeLineCellCommentItemModel *currentCommentItemModel;
  26. @property (nonatomic, strong) NSString *currentCacheFriendId;
  27. @property (nonatomic, strong) SDTimeLineTableHeaderView *headerView;
  28. @end
  29. @implementation SLMomentsVc
  30. {
  31. CGFloat _lastScrollViewOffsetY;
  32. CGFloat _totalKeybordHeight;
  33. }
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. [self confignNavigationBar];
  38. if (@available(iOS 11.0, *)) {
  39. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  40. } else {
  41. self.automaticallyAdjustsScrollViewInsets = NO;
  42. }
  43. if (!self.userModel) {
  44. self.userModel = [SLGlobalInfo SharedInstance].loginInfo.user;
  45. }
  46. self.headerView = [[SDTimeLineTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, 0, 220 * KScaleW)];
  47. self.tableView.tableHeaderView = self.headerView;
  48. self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 250 * KScaleW);
  49. //添加分隔线颜色设置
  50. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  51. [self.tableView registerClass:[SDTimeLineCell class] forCellReuseIdentifier:kTimeLineTableViewCellId];
  52. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(momentsUpdateType:) name:@"SLMomentsUpdateType" object:nil];
  54. [self creatModelsWithCount:0];
  55. if (!self.isDetail && !self.dataArray.count) {
  56. NSString *cacheKey = [NSString stringWithFormat:@"SLMomentsCache--%@--%@", self.userModel.userId,@(self.isFriendList)];
  57. NSString *cacheStr = [[NSUserDefaults standardUserDefaults] objectForKey:cacheKey];
  58. NSArray *items = [[cacheStr mj_JSONObject] objectForKey:@"items"];
  59. if (items.count) {
  60. NSArray<SDTimeLineCellModel *> *models = [SDTimeLineCellModel mj_objectArrayWithKeyValuesArray:items];
  61. self.currentCacheFriendId = models.firstObject.friendId;
  62. if (!self.dataArray.count) {
  63. [self.dataArray addObjectsFromArray:models];
  64. }
  65. }
  66. }
  67. if ([self.userModel.userId isEqualToString:[SLGlobalInfo SharedInstance].loginInfo.user.userId] && !self.isFriendList) {
  68. WS(weakSelf);
  69. [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"loginInfo" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
  70. [weakSelf updateTableViewHeaderView:YES];
  71. }];
  72. }
  73. }
  74. - (void)confignNavigationBar {
  75. self.navigationBar.separatorHeight = 0.0f;
  76. if (!self.isFriendList) {
  77. self.navigationBar.leftBarButtonItem = nil;
  78. UIImage *creatImage =[UIImage imageNamed:@"icon_home_add"];
  79. YMBarButtonItem *rightBarItem =[[YMBarButtonItem alloc]initWithImage:creatImage style:YMBarButtonItemPlain target:self action:@selector(creatBtnClick)];
  80. rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
  81. self.navigationBar.rightBarButtonItem = rightBarItem;
  82. self.navigationBar.backgroundColor = [UIColor clearColor];
  83. self.title = @"朋友圈";
  84. }else {
  85. UIImage *backImage =[UIImage imageNamed:@"icon_back_w"];
  86. YMBarButtonItem *backBarButtonItem =[[YMBarButtonItem alloc]initWithImage:backImage style:YMBarButtonItemPlain target:self action:@selector(backBarButtonPressed:)];
  87. backBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 5, 0, 0);
  88. self.navigationBar.leftBarButtonItem= backBarButtonItem;
  89. }
  90. }
  91. - (void)updateTableViewHeaderView:(BOOL)needReload {
  92. [self.headerView confignHeaderWith:self.userModel];
  93. SLUserModel *userModel = [SLGlobalInfo SharedInstance].loginInfo.user;
  94. if ([self.userModel.userId isEqualToString:userModel.userId]) {
  95. self.userModel = userModel;
  96. [self.headerView updateHeaderViewWith:userModel.momentsMsgNotice];
  97. self.headerView.bgSelImageView.hidden = NO;
  98. if (self.isFriendList) {
  99. self.headerView.frame = CGRectMake(0, 0, 0, 220 * KScaleW);
  100. self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 250 * KScaleW);
  101. }else {
  102. NSInteger unReadCount = [self.userModel.momentsMsgNotice integerValue];
  103. CGFloat h = unReadCount ? 70 : 0;
  104. self.headerView.frame = CGRectMake(0, 0, 0, (220 + h) * KScaleW);
  105. self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, (250 + h) * KScaleW);
  106. self.rdv_tabBarItem.badgeValue = unReadCount < 99 ? [NSString stringWithFormat:@"%ld",unReadCount] : @"99+";
  107. self.rdv_tabBarItem.badgePositionAdjustment = UIOffsetMake(-10, 0);
  108. self.rdv_tabBarItem.badgeTextFont = [UIFont systemFontOfSize:10];
  109. if (needReload) {
  110. [self.tableView reloadData];
  111. }
  112. }
  113. }
  114. }
  115. - (void)viewWillAppear:(BOOL)animated {
  116. [super viewWillAppear:animated];
  117. if ([self.userModel.userId isEqualToString:[SLGlobalInfo SharedInstance].loginInfo.user.userId]) {
  118. self.userModel = [SLGlobalInfo SharedInstance].loginInfo.user;
  119. }
  120. if ([self.tableView.tableHeaderView isKindOfClass:SDTimeLineTableHeaderView.class]) {
  121. [self updateTableViewHeaderView:NO];
  122. }
  123. if (self.tableView.contentOffset.y < 300 && !self.tableView.mj_header.refreshing) {
  124. [self.tableView.mj_header beginRefreshing];
  125. }
  126. }
  127. - (void)viewDidAppear:(BOOL)animated
  128. {
  129. [super viewDidAppear:animated];
  130. [self setupTextField];
  131. }
  132. - (void)viewWillDisappear:(BOOL)animated
  133. {
  134. [_textField resignFirstResponder];
  135. }
  136. - (void)viewDidDisappear:(BOOL)animated {
  137. [super viewDidDisappear:animated];
  138. [_moentsChatView removeFromSuperview];
  139. }
  140. - (void)dealloc
  141. {
  142. [_textField removeFromSuperview];
  143. [[NSNotificationCenter defaultCenter] removeObserver:self];
  144. }
  145. - (void)creatBtnClick {
  146. WS(weakSelf);
  147. [SLCustomizeAlert showBottomAletrWithTitles:@[@"拍照", @"从手机相册选择", @"纯文本"] cancelTitle:@"取消" buttonIndex:^(NSInteger index) {
  148. if (index != 3) {
  149. SLCreatMomentsVc *vc = [SLCreatMomentsVc loadViewControllewWithNib];
  150. vc.photoSelType = index;
  151. vc.creatMomentSuccess = ^{
  152. [weakSelf.tableView.mj_header beginRefreshing];
  153. };
  154. [weakSelf navPushViewController:vc animated:YES];
  155. }
  156. }];
  157. }
  158. - (void)momentsUpdateType:(NSNotification *)notification {
  159. NSDictionary *dict = notification.userInfo;
  160. SDTimeLineCellModel *model = [SDTimeLineCellModel mj_objectWithKeyValues:dict];
  161. __block NSInteger i = 0;
  162. __block SDTimeLineCellModel *cModel;
  163. [self.dataArray enumerateObjectsUsingBlock:^(SDTimeLineCellModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
  164. if ([obj.friendId isEqualToString:model.friendId]) {
  165. i = idx;
  166. cModel = obj;
  167. *stop = YES;
  168. }
  169. }];
  170. if (cModel) {
  171. switch (cModel.updateType) {
  172. case SL_Detail:
  173. {
  174. SLMomentDetailTableVc *vc = [SLMomentsVc loadViewControllewWithNib];
  175. object_setClass(vc, SLMomentDetailTableVc.class);
  176. vc.isDetail = YES;
  177. vc.dataArray = [NSMutableArray arrayWithObject:cModel];
  178. [vc.tableView reloadData];
  179. [self navPushViewController:vc animated:YES];
  180. }
  181. break;
  182. case SL_CommentUpdate:
  183. {
  184. SLMomentsCommentVc *vc = [SLMomentsCommentVc loadViewControllewWithNib];
  185. WS(weakSelf);
  186. vc.momentsCommentBlock = ^(NSString * _Nonnull text) {
  187. [weakSelf updateCommentWithModel:cModel index:[NSIndexPath indexPathForRow:i inSection:0] text:text];
  188. };
  189. [self navPushViewController:vc animated:YES];
  190. }
  191. break;
  192. case SL_Update:
  193. {
  194. [self updateCellLike:cModel index:[NSIndexPath indexPathForRow:i inSection:0]];
  195. }
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. }
  202. - (void)updateCommentWithModel:(SDTimeLineCellModel *)model index:(NSIndexPath *)index text:(NSString *)text {
  203. NSMutableArray *temp = [NSMutableArray new];
  204. [temp addObjectsFromArray:model.list];
  205. NSDictionary *parameter = @{@"commendId" : @"",
  206. @"friendId" : model.friendId,
  207. @"replyUserId" : @"",
  208. @"content" : text ?: @"",
  209. @"level" : @"0",
  210. };
  211. if (!self.isReplayingComment) {
  212. self.currentCommentItemModel = nil;
  213. }else {
  214. parameter = @{@"commendId" : self.currentCommentItemModel.commendId ?: @"",
  215. @"friendId" : self.currentCommentItemModel.friendId ?: @"",
  216. @"replyUserId" : self.currentCommentItemModel.userId ?: @"",
  217. @"content" : text ?: @"",
  218. @"level" : @"0",
  219. };
  220. self.isReplayingComment = NO;
  221. }
  222. [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Friend/InsertComment" parameter:parameter success:^(id responseObject) {
  223. SDTimeLineCellCommentItemModel *commentItemModel = [SDTimeLineCellCommentItemModel new];
  224. SLLoginInfo *targetModel = [SLGlobalInfo SharedInstance].loginInfo;
  225. commentItemModel.userName = targetModel.user.userName;
  226. commentItemModel.userId = targetModel.user.userId;
  227. if (self.currentCommentItemModel) {
  228. commentItemModel.replyUserName = self.currentCommentItemModel.userName;
  229. }
  230. commentItemModel.content = text;
  231. commentItemModel.friendId = model.friendId;
  232. commentItemModel.level = [parameter objectForKey:@"level"];
  233. commentItemModel.commendId = [responseObject objectForKey:@"data"];
  234. [temp addObject:commentItemModel];
  235. model.list = [temp copy];
  236. [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
  237. } failure:^(SPRequestError *error) {
  238. }];
  239. }
  240. - (void)setupTextField
  241. {
  242. self.moentsChatView = [[SLMoentsChatView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, self.view.width_sd, textFieldH)];
  243. self.moentsChatView.backgroundColor = SLColor(@"#F5F5F5");
  244. [[UIApplication sharedApplication].keyWindow addSubview:self.moentsChatView];
  245. _textField = self.moentsChatView.nextGrowingTextView.textView;
  246. _textField.returnKeyType = UIReturnKeyDone;
  247. self.moentsChatView.nextGrowingTextView.delegates = self;
  248. WS(weakSelf);
  249. self.moentsChatView.sendTextBlock = ^(NSString * _Nonnull text) {
  250. if (!text.length) {
  251. return;
  252. }
  253. [weakSelf.textField resignFirstResponder];
  254. [weakSelf.view endEditing:YES];
  255. SDTimeLineCellModel *model = weakSelf.dataArray[weakSelf.currentEditingIndexthPath.row];
  256. [weakSelf updateCommentWithModel:model index:weakSelf.currentEditingIndexthPath text:text];
  257. weakSelf.textField.text = @"";
  258. };
  259. }
  260. - (void)creatModelsWithCount:(NSInteger)count
  261. {
  262. WS(weakSelf);
  263. [self.tableView tableviewRefresh:self.dataArray TableViewRefresh:^(NSInteger pageNo, TableListCount listCount) {
  264. NSDictionary *parameter = @{@"PageIndex": @(pageNo),
  265. @"PageSize": @(SLPageSize),
  266. @"type" : @"0",
  267. @"userId" : self.userModel.userId,
  268. };
  269. NSString *url = weakSelf.isFriendList ? @"/api/Friend/GetUserFriendList" : @"/api/Friend/GetFriendList";
  270. [[SLHttpCenter SharedInstance] getWithUrl:url parameter:parameter success:^(id responseObject) {
  271. NSDictionary *data = [responseObject objectForKey:@"data"];
  272. [SLGlobalInfo SharedInstance].loginInfo.user.momentsMsgNotice = [[data objectForKey:@"unread"] stringValue];
  273. [self updateTableViewHeaderView:NO];
  274. NSArray *items = [data objectForKey:@"items"];
  275. if (items.count) {
  276. NSMutableArray *marr = [SDTimeLineCellModel mj_objectArrayWithKeyValuesArray:items];
  277. [marr enumerateObjectsUsingBlock:^(SDTimeLineCellModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  278. obj.likeItemsArray = [NSMutableArray new];
  279. [obj.likeStr enumerateObjectsUsingBlock:^(id _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop) {
  280. SDTimeLineCellLikeItemModel *model = [[SDTimeLineCellLikeItemModel alloc] init];
  281. model.userName = obj1;
  282. model.userId = @"1111";
  283. [obj.likeItemsArray addObject:model];
  284. }];
  285. }];
  286. [weakSelf.dataArray addObjectsFromArray:marr];
  287. dispatch_async(dispatch_get_main_queue(), ^{
  288. [weakSelf.tableView reloadData];
  289. SDTimeLineCellModel *firstObj = weakSelf.dataArray.firstObject;
  290. if (pageNo == 1 && ![firstObj.friendId isEqualToString:weakSelf.currentCacheFriendId]) {
  291. weakSelf.currentCacheFriendId = firstObj.friendId;
  292. NSString *cacheKey = [NSString stringWithFormat:@"SLMomentsCache--%@--%@", weakSelf.userModel.userId,@(weakSelf.isFriendList)];
  293. [[NSUserDefaults standardUserDefaults] setObject:[data mj_JSONString] forKey:cacheKey];
  294. [[NSUserDefaults standardUserDefaults] synchronize];
  295. }
  296. });
  297. [weakSelf.dataArray enumerateObjectsUsingBlock:^(SDTimeLineCellModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
  298. if ([obj.friendImgUrl componentsSeparatedByString:@","].count == 1) {
  299. [weakSelf.KVOController observe:obj keyPath:@"imageSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
  300. dispatch_async(dispatch_get_main_queue(), ^{
  301. [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:idx inSection:0], nil] withRowAnimation:UITableViewRowAnimationAutomatic];
  302. });
  303. }];
  304. }
  305. }];
  306. }
  307. listCount([items count]);
  308. } failure:^(SPRequestError *error) {
  309. listCount(-1);
  310. }];
  311. }];
  312. }
  313. #pragma -mark scrollViewDelegate
  314. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  315. {
  316. UIColor * color = [UIColor whiteColor];
  317. CGFloat offsetY = scrollView.contentOffset.y;
  318. CGFloat alpha = 1.0;
  319. if (offsetY >= 2) {
  320. alpha = MIN(1, 1 - ((2 + kNavBarHeight - offsetY) / kNavBarHeight));
  321. self.navigationBar.backgroundColor = [color colorWithAlphaComponent:alpha];
  322. self.navigationBar.rightBarButtonItemView.itemImageView.image = ImageName(@"icon_pyq_more_back");
  323. self.navigationBar.titleItemView.alpha = alpha;
  324. self.navigationBar.leftBarButtonItemView.itemImageView.image = ImageName(@"icon_back_b");
  325. } else {
  326. self.navigationBar.titleItemView.alpha = 0;
  327. self.navigationBar.backgroundColor = [color colorWithAlphaComponent:0];
  328. self.navigationBar.rightBarButtonItemView.itemImageView.image = ImageName(@"icon_pyq_more");
  329. self.navigationBar.leftBarButtonItemView.itemImageView.image = ImageName(@"icon_back_w");
  330. }
  331. }
  332. #pragma mark - tableviewDelegate
  333. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  334. {
  335. return self.dataArray.count;
  336. }
  337. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  338. {
  339. SDTimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:kTimeLineTableViewCellId];
  340. cell.indexPath = indexPath;
  341. SDTimeLineCellModel *model = self.dataArray[indexPath.row];
  342. cell.model = model;
  343. __weak typeof(self) weakSelf = self;
  344. if (!cell.moreButtonClickedBlock) {
  345. [cell setMoreButtonClickedBlock:^(NSIndexPath *indexPath) {
  346. SDTimeLineCellModel *model = weakSelf.dataArray[indexPath.row];
  347. model.isOpening = !model.isOpening;
  348. [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  349. // [weakSelf.tableView reloadData];
  350. }];
  351. //评论
  352. [cell setDidClickCommentLabelBlock:^(NSString *commentId, CGRect rectInWindow, NSIndexPath *indexPath1) {
  353. weakSelf.currentEditingIndexthPath = [NSIndexPath indexPathForRow:indexPath1.row inSection:0];
  354. SDTimeLineCellModel *editModel = weakSelf.dataArray[indexPath1.row];
  355. [weakSelf.textField becomeFirstResponder];
  356. weakSelf.isReplayingComment = YES;
  357. weakSelf.currentCommentItemModel = [editModel.list objectAtIndex:indexPath1.section];
  358. weakSelf.commentToUser = commentId;
  359. [weakSelf adjustTableViewToFitKeyboard];
  360. }];
  361. //长按删除评论
  362. [cell setDidLongTouchCommentLabelBlock:^(NSString *commentId, CGRect rectInWindow, NSIndexPath *indexPath1) {
  363. //indexPath1 row == table row Section == SDTimeLineCellCommentItemModel.list
  364. SDTimeLineCellModel *editModel = [weakSelf.dataArray objectAtIndex:indexPath1.row];
  365. if ([editModel.userId isEqualToString:[SLGlobalInfo SharedInstance].loginInfo.user.userId]) {
  366. NSMutableArray *temp = [NSMutableArray new];
  367. [temp addObjectsFromArray:editModel.list];
  368. SDTimeLineCellCommentItemModel *commentModel = [editModel.list objectAtIndex:indexPath1.section];
  369. [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Friend/DelateComment" parameter:@{@"commentId":commentModel.commendId} success:^(id responseObject) {
  370. [temp removeObjectAtIndex:indexPath1.section];
  371. editModel.list = [temp copy];
  372. [weakSelf.tableView reloadData];
  373. } failure:^(SPRequestError *error) {
  374. }];
  375. }
  376. }];
  377. cell.delegate = self;
  378. }
  379. ////// 此步设置用于实现cell的frame缓存,可以让tableview滑动更加流畅 //////
  380. [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
  381. ///////////////////////////////////////////////////////////////////////
  382. return cell;
  383. }
  384. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  385. {
  386. }
  387. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  388. {
  389. // >>>>>>>>>>>>>>>>>>>>> * cell自适应 * >>>>>>>>>>>>>>>>>>>>>>>>
  390. if (indexPath.row > self.dataArray.count - 1) {
  391. return 0.0f;
  392. }
  393. id model = self.dataArray[indexPath.row];
  394. return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[SDTimeLineCell class] contentViewWidth:[self cellContentViewWith]];
  395. }
  396. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  397. return 0.001f;
  398. }
  399. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  400. return 0.001f;
  401. }
  402. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  403. {
  404. [_textField resignFirstResponder];
  405. }
  406. - (CGFloat)cellContentViewWith
  407. {
  408. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  409. // 适配ios7横屏
  410. if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
  411. width = [UIScreen mainScreen].bounds.size.height;
  412. }
  413. return width;
  414. }
  415. #pragma mark - SDTimeLineCellDelegate
  416. - (void)didClickcCommentButtonInCell:(UITableViewCell *)cell
  417. {
  418. [_textField becomeFirstResponder];
  419. _currentEditingIndexthPath = [self.tableView indexPathForCell:cell];
  420. [self adjustTableViewToFitKeyboard];
  421. }
  422. - (void)didClickLikeButtonInCell:(UITableViewCell *)cell
  423. {
  424. NSIndexPath *index = [self.tableView indexPathForCell:cell];
  425. SDTimeLineCellModel *model = self.dataArray[index.row];
  426. model.updateType = SL_Update;
  427. [[NSNotificationCenter defaultCenter] postNotificationName:@"SLMomentsUpdateType" object:nil userInfo:[model mj_JSONObject]];
  428. // [self updateCellLike:model index:index];
  429. }
  430. - (void)didClickDelButtonInCell:(UITableViewCell *)cell
  431. {
  432. [SLCustomizeAlert showAletrWithTitle:@"温馨提示" message:@"是否要删除当条朋友圈内容" sureBtnTitle:@"确定" cancelBtnTitle:@"取消" sureBtnAction:^{
  433. NSIndexPath *index = [self.tableView indexPathForCell:cell];
  434. SDTimeLineCellModel *model = self.dataArray[index.row];
  435. [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Friend/DelateFriendCircle" parameter:@{@"id":model.friendId} success:^(id responseObject) {
  436. [self.dataArray removeObject:model];
  437. [self.tableView reloadData];
  438. } failure:^(SPRequestError *error) {
  439. }];
  440. } cancelBtnAction:^{
  441. }];
  442. }
  443. - (void)didClickTransInCell:(UITableViewCell *)cell {
  444. NSIndexPath *index = [self.tableView indexPathForCell:cell];
  445. [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
  446. }
  447. - (void)updateCellLike:(SDTimeLineCellModel *)model index:(NSIndexPath *)index {
  448. NSMutableArray *temp = [NSMutableArray arrayWithArray:model.likeItemsArray];
  449. if ([self isEqual:[UIViewController getCurrentShowVC]]) {
  450. [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Friend/LikeHe" parameter:@{@"friendId":model.friendId} success:^(id responseObject) {
  451. } failure:^(SPRequestError *error) {
  452. }];
  453. }
  454. SLUserModel *userModel = [SLGlobalInfo SharedInstance].loginInfo.user;
  455. if (!model.isLike) {
  456. SDTimeLineCellLikeItemModel *likeModel = [SDTimeLineCellLikeItemModel new];
  457. likeModel.userName = userModel.userName;
  458. likeModel.userId = userModel.userId;
  459. [temp addObject:likeModel];
  460. model.isLike = YES;
  461. } else {
  462. SDTimeLineCellLikeItemModel *tempLikeModel = nil;
  463. for (SDTimeLineCellLikeItemModel *likeModel in model.likeItemsArray) {
  464. if ([likeModel.userName isEqualToString:userModel.userName]) {
  465. tempLikeModel = likeModel;
  466. break;
  467. }
  468. }
  469. [temp removeObject:tempLikeModel];
  470. model.isLike = NO;
  471. }
  472. model.likeItemsArray = [temp copy];
  473. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  474. [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
  475. });
  476. }
  477. #pragma mark - Keyboard
  478. - (void)adjustTableViewToFitKeyboard
  479. {
  480. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  481. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:_currentEditingIndexthPath];
  482. CGRect rect = [cell.superview convertRect:cell.frame toView:window];
  483. [self adjustTableViewToFitKeyboardWithRect:rect];
  484. }
  485. - (void)adjustTableViewToFitKeyboardWithRect:(CGRect)rect
  486. {
  487. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  488. CGFloat delta = CGRectGetMaxY(rect) - (window.bounds.size.height - _totalKeybordHeight);
  489. CGPoint offset = self.tableView.contentOffset;
  490. offset.y += delta;
  491. if (offset.y < 0) {
  492. offset.y = 0;
  493. }
  494. [self.tableView setContentOffset:offset animated:YES];
  495. }
  496. - (void)keyboardNotification:(NSNotification *)notification
  497. {
  498. NSDictionary *dict = notification.userInfo;
  499. CGRect rect = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
  500. CGRect textFieldRect = CGRectMake(0, rect.origin.y - textFieldH, rect.size.width, textFieldH);
  501. if (rect.origin.y == [UIScreen mainScreen].bounds.size.height) {
  502. textFieldRect = rect;
  503. }
  504. [UIView animateWithDuration:0.25 animations:^{
  505. self.moentsChatView.frame = textFieldRect;
  506. }];
  507. CGFloat h = rect.size.height + textFieldH;
  508. if (_totalKeybordHeight != h) {
  509. _totalKeybordHeight = h;
  510. [self adjustTableViewToFitKeyboard];
  511. }
  512. }
  513. - (void)willChangeHeight:(CGFloat)height
  514. {
  515. [self bottomChangeHeight:height];
  516. }
  517. - (void)didChangeHeight:(CGFloat)height
  518. {
  519. [self bottomChangeHeight:height];
  520. }
  521. - (void)bottomChangeHeight:(CGFloat)height
  522. {
  523. CGRect rect = self.moentsChatView.frame;
  524. CGFloat h = MAX(height + 16, 56);
  525. rect.origin.y = rect.origin.y - (h - rect.size.height);
  526. rect.size.height = h;
  527. self.moentsChatView.frame = rect;
  528. [self.moentsChatView setNeedsDisplay];
  529. }
  530. - (NSMutableArray *)dataArray {
  531. if (!_dataArray) {
  532. _dataArray = [NSMutableArray array];
  533. }
  534. return _dataArray;
  535. }
  536. @end