SiteTableViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // SiteTableViewController.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/25.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "SiteTableViewController.h"
  9. #import "PlaceModel.h"
  10. #import "SitTabCell.h"
  11. static NSString *const cellID = @"cellID";
  12. @interface SiteTableViewController ()
  13. {
  14. //左边被选中的索引值
  15. NSInteger _selectIndex;
  16. }
  17. /*数据源*/
  18. @property(nonatomic,strong)NSMutableArray *dataArray;
  19. @end
  20. @implementation SiteTableViewController
  21. - (void)viewWillAppear:(BOOL)animated {
  22. [super viewWillAppear:animated];
  23. //选中第一个
  24. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  25. //
  26. [DataSourceManager getSiteWithUrlPagesize:100 Page:1 completionBlock:^(NSMutableArray *array) {
  27. NSLog(@"%@----array",array);
  28. _dataArray = array;
  29. [self.tableView reloadData];
  30. }];
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  35. self.tableView.showsHorizontalScrollIndicator =NO;
  36. self.tableView.showsVerticalScrollIndicator = NO;
  37. [self.tableView registerClass:[SitTabCell class] forCellReuseIdentifier:cellID];
  38. self.tableView.backgroundColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1];
  39. // _dataArray=[[NSMutableArray alloc]init];
  40. _selectIndex =0;
  41. [DataSourceManager getSiteWithUrlPagesize:100 Page:1 completionBlock:^(NSMutableArray *array) {
  42. NSLog(@"%@----array",array);
  43. _dataArray = array;
  44. if (_dataArray.count != 0) {
  45. PlaceModel *model = _dataArray[0];
  46. NSLog(@"--------%@",model.Id);
  47. NSArray *infoArr = @[model.Id,model.location];
  48. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:infoArr,@"siteID", nil];
  49. [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeSiteID" object:nil userInfo:dic];
  50. }
  51. [self.tableView reloadData];
  52. }];
  53. }
  54. - (void)didReceiveMemoryWarning {
  55. [super didReceiveMemoryWarning];
  56. // Dispose of any resources that can be recreated.
  57. }
  58. #pragma mark - Table view data source
  59. //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  60. // return 1;
  61. //}
  62. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  63. return _dataArray.count;
  64. }
  65. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  66. return 54;
  67. }
  68. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  69. return 44;
  70. }
  71. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  72. SitTabCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
  73. //让单元格的分割线和Cell一样宽
  74. cell.preservesSuperviewLayoutMargins = false;
  75. cell.separatorInset = UIEdgeInsetsZero;
  76. cell.layoutMargins = UIEdgeInsetsZero;
  77. cell.model = self.dataArray[indexPath.row];
  78. cell.botlabel.textColor = SXUIColorFromRGB(0x444444);
  79. cell.botlabel.numberOfLines = 2;
  80. cell.botlabel.font = [UIFont systemFontOfSize:18];
  81. //设置selectedBackgroundView
  82. cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
  83. cell.selectedBackgroundView.backgroundColor = [UIColor orangeColor];
  84. //textLabel的选中状态
  85. cell.textLabel.highlightedTextColor = [UIColor whiteColor];
  86. // 另外需要默认指定第一行时,可在添加
  87. if (indexPath.row==_selectIndex) {//指定第一行为选中状态
  88. [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
  89. }
  90. cell.backgroundColor = [UIColor whiteColor];
  91. return cell;
  92. }
  93. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  94. _selectIndex = indexPath.row;
  95. //点击的时候传值
  96. PlaceModel *model = self.dataArray[_selectIndex];
  97. [DataSourceManager getDeveceWithUrlPagesize:100 SiteId:model.Id Page:1 completionBlock:^(NSMutableArray *array) {
  98. NSLog(@"zheshi1%@",array);
  99. if (self.delegate && [self.delegate respondsToSelector:@selector(siteViewContller:didSelectRowWithData:)]) {
  100. [self.delegate siteViewContller:self didSelectRowWithData:array];
  101. }
  102. NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:model.location,@"location", nil];
  103. [[NSNotificationCenter defaultCenter]postNotificationName:@"location" object:nil userInfo:dic];
  104. }];
  105. }
  106. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  107. UIView *headerView = [[UIView alloc]init];
  108. headerView.backgroundColor = [UIColor grayColor];
  109. UILabel *siteLabel = [[UILabel alloc]init];
  110. siteLabel.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44);
  111. siteLabel.text = @"场所列表";
  112. siteLabel.font = [UIFont systemFontOfSize:18];
  113. siteLabel.textColor = [UIColor whiteColor];
  114. siteLabel.textAlignment = NSTextAlignmentCenter;
  115. [headerView addSubview:siteLabel];
  116. return headerView;
  117. }
  118. - (void)setDelegate:(id<SiteTableViewControllerDelegate>)delegate {
  119. _delegate = delegate;
  120. /*
  121. 1:默认表格的某一行被选中,不会调用didSelectRowAtIndexPath方法,只是设置cell的选中的一个状态
  122. 2:获得tableView选中的某一行的indexpath:self.tableView.indexPathForSelectedRow
  123. // */
  124. // [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
  125. //
  126. }
  127. /*
  128. 1:两方法分别是:视图即将出现的时候调用,视图已经出现的时候调用
  129. 2:当某个控制器被压入栈里,或是被moda的控制器遮住的时候,返回到此控制器,则两个方法依然会被调用
  130. */
  131. @end