ContactTableViewCell.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // ContactTableViewCell.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/29.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "ContactTableViewCell.h"
  9. #import "User.h"
  10. #import "UIView+NTES.h"
  11. #import "NIMAvatarImageView.h"
  12. #import "AddFriendViewController.h"
  13. #import "HttpRequest.h"
  14. #import "UIView+Toast.h"
  15. @interface ContactTableViewCell()
  16. @property (weak, nonatomic) IBOutlet UIView *avatarContainer;
  17. @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
  18. @property (weak, nonatomic) IBOutlet UILabel *descLabel;
  19. @property (weak, nonatomic) IBOutlet UIButton *actionBtn;
  20. @property (nonatomic, strong) NIMAvatarImageView *avatar;
  21. @property (nonatomic, strong) ContactData *data;
  22. @end
  23. @implementation ContactTableViewCell
  24. - (void)awakeFromNib {
  25. [super awakeFromNib];
  26. // Initialization code
  27. _actionBtn.layer.cornerRadius = 3;
  28. [self setData:_data];
  29. }
  30. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  31. [super setSelected:selected animated:animated];
  32. // Configure the view for the selected state
  33. }
  34. - (void)setData:(ContactData *)data{
  35. _data = data;
  36. if(!data)
  37. return;
  38. if(!_avatar)
  39. {
  40. _avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, _avatarContainer.width, _avatarContainer.height)];
  41. [_avatarContainer addSubview:_avatar];
  42. }
  43. NSURL *url = nil;
  44. if(data.userId)
  45. {
  46. NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:data.userId];
  47. url = user.userInfo && user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
  48. _nameLabel.text = data.name;
  49. BOOL isFollow = [[User sharedInfo] isFollowd:data.userId];
  50. if(isFollow)
  51. {
  52. [_actionBtn setBackgroundColor:UIColor.clearColor];
  53. [_actionBtn setTitle:@"已添加" forState:UIControlStateNormal];
  54. [_actionBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  55. [_actionBtn setUserInteractionEnabled:NO];
  56. }
  57. else
  58. {
  59. [_actionBtn setBackgroundColor:User.greenColor];
  60. [_actionBtn setTitle:@"添加" forState:UIControlStateNormal];
  61. [_actionBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
  62. [_actionBtn setUserInteractionEnabled:YES];
  63. }
  64. if(data.follow > 0)
  65. {
  66. _descLabel.text = [NSString stringWithFormat:@"%@已经关注了你", data.name];
  67. }
  68. else
  69. {
  70. _descLabel.text = [data.phones objectAtIndex:0];
  71. }
  72. }
  73. else
  74. {
  75. _nameLabel.text = data.name;
  76. _descLabel.text = [data.phones objectAtIndex:0];
  77. if(_data.msgSend)
  78. {
  79. [_actionBtn setBackgroundColor:UIColor.clearColor];
  80. [_actionBtn setTitle:@"已发送" forState:UIControlStateNormal];
  81. [_actionBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  82. [_actionBtn setUserInteractionEnabled:NO];
  83. }
  84. else
  85. {
  86. [_actionBtn setBackgroundColor:User.orangeColor];
  87. [_actionBtn setTitle:@"邀请" forState:UIControlStateNormal];
  88. [_actionBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
  89. [_actionBtn setUserInteractionEnabled:YES];
  90. }
  91. }
  92. [_avatar nim_setImageWithURL:url placeholderImage:User.defaultUserAvatar];
  93. }
  94. - (IBAction)onTouchActionBtn:(id)sender {
  95. if(_data.userId)
  96. {
  97. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  98. AddFriendViewController *vc = [board instantiateViewControllerWithIdentifier:@"AddFriend"];
  99. [vc setUserId:_data.userId];
  100. [self.viewController.navigationController pushViewController:vc animated:YES];
  101. }
  102. else
  103. {
  104. [[HttpRequest shared] sendMsg:_data.phones[0] success:^{
  105. [self makeToast:@"发送成功" duration:2.0 position:CSToastPositionCenter];
  106. _data.msgSend = YES;
  107. [self setData:_data];
  108. } failure:^{
  109. [self makeToast:@"发送失败,请重试" duration:2.0 position:CSToastPositionCenter];
  110. }];
  111. }
  112. }
  113. @end