PlaceList_VC.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // PlaceList_VC.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/4.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "PlaceList_VC.h"
  9. #import "Header.h"
  10. @interface PlaceList_VC ()<UITableViewDelegate,UITableViewDataSource>
  11. {
  12. NSInteger _selectIndex;
  13. NSString *siteID;
  14. }
  15. @property(nonatomic,strong)NSMutableArray *dataArray;
  16. @property(nonatomic,strong)NSIndexPath *index;
  17. @property(nonatomic,assign)NSInteger indexNum;
  18. @end
  19. @implementation PlaceList_VC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. _dataArray = [NSMutableArray array];
  23. self.navigationItem.title = @"场所列表";
  24. [self setupUI];
  25. [DataSourceManager getSiteWithUrlPagesize:100 Page:1 completionBlock:^(NSMutableArray *array) {
  26. _dataArray = array;
  27. [_tableView reloadData];
  28. }];
  29. //集成下拉刷新控件
  30. [self setupDownRefresh];
  31. }
  32. /**
  33. * 集成下拉刷新控件
  34. */
  35. - (void)setupDownRefresh
  36. {
  37. // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
  38. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDatas)];
  39. //马上进入刷新状态
  40. [self.tableView.mj_header beginRefreshing];
  41. }
  42. -(void)loadNewDatas{
  43. // _pageIndex =1;//默认加载第一页
  44. [self.dataArray removeAllObjects];
  45. //请求数据
  46. [DataSourceManager getSiteWithUrlPagesize:100 Page:1 completionBlock:^(NSMutableArray *array) {
  47. _dataArray = array;
  48. [_tableView reloadData];
  49. //结束刷新
  50. [self.tableView.mj_header endRefreshing];
  51. }];
  52. }
  53. -(void)setupUI{
  54. self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1];
  55. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
  56. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  57. [backBtn setImage:[UIImage imageNamed:@"hsh_return"] forState:UIControlStateNormal];
  58. backBtn.frame = CGRectMake(0, 0, 44, 44);
  59. [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  60. backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  61. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
  62. //
  63. // rightButton.frame = CGRectMake(0, 0, 22, 22);
  64. // [rightButton setImage:[UIImage imageNamed:@"hsh_user_detail"] forState:UIControlStateNormal];
  65. //// [rightButton setTitle:@"删除" forState:UIControlStateNormal];
  66. // [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  67. //// UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  68. //// self.navigationItem.rightBarButtonItem = rightItem;
  69. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
  70. _tableView.delegate = self;
  71. _tableView.dataSource = self;
  72. [self.view addSubview:_tableView];
  73. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  74. }
  75. -(void)backClick{
  76. [self.navigationController popViewControllerAnimated:YES];
  77. }
  78. #pragma mark---tableviewDataSource
  79. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  80. return _dataArray.count;
  81. }
  82. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  83. return 63;
  84. }
  85. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  86. static NSString *celld = @"cell";
  87. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celld];
  88. if (!cell) {
  89. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:celld];
  90. }
  91. // UIView *backView = [[UIView alloc]init];
  92. // backView.tag =indexPath.row;
  93. // backView.frame = CGRectMake(60, 0, self.view.frame.size.width, 63);
  94. // UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action:)];
  95. //
  96. // [backView addGestureRecognizer:tap];
  97. // [cell.contentView addSubview:backView];
  98. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  99. PlaceModel *model = [self.dataArray objectAtIndex:indexPath.row];
  100. cell.textLabel.text = model.deployment;
  101. cell.detailTextLabel.text = model.location;
  102. UIView *line = [[UIView alloc]init];
  103. line.frame = CGRectMake(0, 60, WIDTH, 3);
  104. line.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
  105. [cell.contentView addSubview:line];
  106. //添加长按删除
  107. UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
  108. //设定最小的长按时间 按不够这个时间不响应手势
  109. longPressGR.minimumPressDuration = 1;
  110. [cell.contentView addGestureRecognizer:longPressGR];
  111. return cell;
  112. }
  113. //判断表视图是否编辑的时候让跳转
  114. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  115. PlaceModel *model = [self.dataArray objectAtIndex:indexPath.row];
  116. siteID = model.Id;
  117. _selectIndex =indexPath.row;
  118. PlaceDetail_VC *detailVC = [[PlaceDetail_VC alloc]init];
  119. detailVC.siteName =model.deployment;
  120. detailVC.titleRegionalism = model.regionName;
  121. detailVC.titleLocation = model.location;
  122. detailVC.siteID = model.Id;
  123. [self.navigationController pushViewController:detailVC animated:YES];
  124. }
  125. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  126. return YES;
  127. }
  128. //选择编辑的方式,按照选择的方式对表进行处理
  129. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  130. if (editingStyle == UITableViewCellEditingStyleDelete) {
  131. }
  132. }
  133. - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{
  134. // 添加一个删除按钮
  135. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  136. PlaceModel *model = [self.dataArray objectAtIndex:indexPath.row];
  137. [DataSourceManager deleteSiteWithUrlSiteID:model.Id completionBlock:^(NSDictionary *dic) {
  138. // 删除数据源
  139. [self.dataArray removeObjectAtIndex:_selectIndex];
  140. // 或者cell删除
  141. // [tableView deleteRowsAtIndexPaths:@[_selectIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
  142. [_tableView reloadData];
  143. }];
  144. }];
  145. return @[deleteRowAction];
  146. }
  147. //选择你要对表进行处理的方式 默认是删除方式
  148. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  149. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  150. }
  151. -(void)action:(UIGestureRecognizer *)recognizer{
  152. UITapGestureRecognizer *tap = (UITapGestureRecognizer *)recognizer;
  153. NSInteger tag = tap.view.tag;
  154. PlaceModel *model = [self.dataArray objectAtIndex:tag];
  155. siteID = model.Id;
  156. _selectIndex = tag;
  157. PlaceDetail_VC *detailVC = [[PlaceDetail_VC alloc]init];
  158. detailVC.siteName =model.deployment;
  159. detailVC.titleRegionalism = model.regionName;
  160. detailVC.titleLocation = model.location;
  161. detailVC.siteID = model.Id;
  162. [self.navigationController pushViewController:detailVC animated:YES];
  163. }
  164. - (void)didReceiveMemoryWarning {
  165. [super didReceiveMemoryWarning];
  166. // Dispose of any resources that can be recreated.
  167. }
  168. /*
  169. #pragma mark - Navigation
  170. // In a storyboard-based application, you will often want to do a little preparation before navigation
  171. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  172. // Get the new view controller using [segue destinationViewController].
  173. // Pass the selected object to the new view controller.
  174. }
  175. */
  176. -(void)confirmBtnClick{
  177. //可以得到此时你点击的哪一行
  178. self.tableView.allowsMultipleSelectionDuringEditing = YES;
  179. self.tableView.editing = !self.tableView.editing;
  180. }
  181. //实现手势对应的功能
  182. -(void)lpGR:(UILongPressGestureRecognizer *)lpGR
  183. {
  184. if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
  185. CGPoint point = [lpGR locationInView:self.tableView];
  186. self.index = [self.tableView indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
  187. self.indexNum = self.index.row;
  188. }
  189. if (lpGR.state == UIGestureRecognizerStateEnded)//手势结束
  190. {
  191. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
  192. [alert show];
  193. }
  194. }
  195. //提示框代理方法
  196. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  197. if (buttonIndex == 0) {
  198. NSLog(@"这是场所的id%@",siteID);
  199. //有问题
  200. [DataSourceManager deleteSiteWithUrlSiteID:siteID completionBlock:^(NSDictionary *dic) {
  201. // 删除数据源
  202. [self.dataArray removeObjectAtIndex:_selectIndex];
  203. // 或者cell删除
  204. // [tableView deleteRowsAtIndexPaths:@[_selectIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
  205. [_tableView reloadData];
  206. }];
  207. // [self.dataArray removeObjectAtIndex:self.indexNum];
  208. }else{
  209. }
  210. }
  211. @end