new_bgmListView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // new_bgmListView.m
  3. // BuguLive
  4. //
  5. // Created by bugu on 2019/5/25.
  6. // Copyright © 2019年 xfg. All rights reserved.
  7. //
  8. #import "new_bgmListView.h"
  9. //#import "Music_manager.h"
  10. @interface new_bgmListView ()<UITableViewDelegate,UITableViewDataSource,new_bgmitemDelegate>
  11. {
  12. int _page;
  13. }
  14. @property (nonatomic, strong)NSMutableArray *datasource;
  15. @property (nonatomic, strong)music_obj *waitObj;
  16. @end
  17. @implementation new_bgmListView
  18. - (instancetype)init
  19. {
  20. self =[super init];
  21. _page =1;
  22. return self;
  23. }
  24. - (void)mp3Play:(music_obj *)model
  25. {
  26. __weak typeof(self) weakself =self;
  27. [self addVideoWithPlayerLayerFrame:CGRectZero withPlayerItemUrlString:model.music_url complete:^(AVPlayer *player) {
  28. weakself.player =player;
  29. [weakself.player play];
  30. }];
  31. }
  32. - (void)setDatasource:(NSArray *)datasource andPage:(int)page
  33. {
  34. if (page > _page)
  35. {
  36. _page =page;
  37. for (id obj in datasource)
  38. {
  39. music_obj *model =[music_obj mj_objectWithKeyValues:obj];
  40. [self.datasource addObject:model];
  41. }
  42. }else
  43. {
  44. [self.datasource removeAllObjects];
  45. for (id obj in datasource)
  46. {
  47. music_obj *model =[music_obj mj_objectWithKeyValues:obj];
  48. [self.datasource addObject:model];;
  49. }
  50. }
  51. [self.tableview reloadData];
  52. }
  53. #pragma 代理
  54. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  55. {
  56. new_bgmitemCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
  57. if (!cell)
  58. {
  59. cell =[[new_bgmitemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  60. cell.selectionStyle =UITableViewCellSelectionStyleNone;
  61. }
  62. cell.delegate =self;
  63. cell.model =self.datasource[indexPath.row];
  64. return cell;
  65. }
  66. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  67. {
  68. return 1;
  69. }
  70. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  71. {
  72. return self.datasource.count;
  73. }
  74. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  75. {
  76. music_obj *model =self.datasource[indexPath.row];
  77. if (model.isselect)
  78. {
  79. return 140 +104;
  80. }else
  81. {
  82. return 140;
  83. }
  84. }
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. music_obj *model =self.datasource[indexPath.row];
  88. //点击播放试听,展开cell
  89. for (music_obj *obj in self.datasource)
  90. {
  91. if (obj == model)
  92. {
  93. if(!obj.isselect)
  94. {
  95. obj.isselect =YES;
  96. if (_waitObj == obj)
  97. {
  98. [self.player play];
  99. }else{
  100. [self mp3Play:obj];
  101. }
  102. _waitObj =nil;
  103. }else
  104. {
  105. obj.isselect =NO;
  106. [self.player pause];
  107. _waitObj =obj;
  108. }
  109. }else
  110. {
  111. obj.isselect =NO;
  112. }
  113. }
  114. [self.tableview reloadData];
  115. }
  116. - (void)selectItem:(music_obj *)model
  117. {
  118. if ([_delegate respondsToSelector:@selector(SureUseobj:)])
  119. {
  120. [_delegate SureUseobj:model];
  121. }
  122. }
  123. - (void)doCollection:(music_obj *)model
  124. {
  125. // [Music_manager MusicManagerdoCollection:model.id andcallback:^(id response) {
  126. // if (response)
  127. // {
  128. // model.is_collection =1;
  129. // [self.tableview reloadData];
  130. // }
  131. // }];
  132. }
  133. #pragma 懒加载
  134. - (UITableView *)tableview
  135. {
  136. if (!_tableview)
  137. {
  138. _tableview =[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  139. [self addSubview:_tableview];
  140. _tableview.delegate =self;
  141. _tableview.dataSource =self;
  142. _tableview.separatorStyle =UITableViewCellSeparatorStyleNone;
  143. [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.center.width.height.equalTo(self);
  145. }];
  146. }
  147. return _tableview;
  148. }
  149. - (NSMutableArray *)datasource
  150. {
  151. if (!_datasource)
  152. {
  153. _datasource =[NSMutableArray new];
  154. }
  155. return _datasource;
  156. }
  157. /*
  158. // Only override drawRect: if you perform custom drawing.
  159. // An empty implementation adversely affects performance during animation.
  160. - (void)drawRect:(CGRect)rect {
  161. // Drawing code
  162. }
  163. */
  164. @end