popViewController.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // popViewController.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/3.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "popViewController.h"
  9. @interface popViewController ()<UITableViewDataSource,UITableViewDelegate>
  10. @property (nonatomic,strong)UITableView *tableView;
  11. @end
  12. @implementation popViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
  16. [self.view addSubview:self.tableView];
  17. self.tableView.dataSource = self;
  18. self.tableView.delegate = self;
  19. self.tableView.scrollEnabled = NO;
  20. }
  21. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  22. return 2;
  23. }
  24. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  25. static NSString *cellid = @"cellID";
  26. UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellid];
  27. if (!cell) {
  28. cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
  29. }
  30. cell.imageView.image = [UIImage imageNamed:@"hsh_home_add"];
  31. NSArray *title = @[@"添加设备",@"添加场所"];
  32. cell.textLabel.text = title[indexPath.row];
  33. return cell;
  34. }
  35. //-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  36. // return 44;
  37. //}
  38. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  39. //创建消息,在点击相应的颜色时发送,在ViewController中接受消息并做出相应的处理
  40. [[NSNotificationCenter defaultCenter] postNotificationName:@"click" object:indexPath];
  41. }
  42. //重写preferredContentSize(iOS7之后)来返回最合适的大小,如果不重写,会返回一整个tableview尽管下面一部分cell是没有内容的,重写后只会返回有内容的部分,我这里还修改了宽,让它窄一点。可以尝试注释这一部分的代码来看效果,通过修改返回的size得到你期望的popover的大小。
  43. - (CGSize)preferredContentSize {
  44. if (self.presentingViewController && self.tableView != nil) {
  45. CGSize tempSize = self.presentingViewController.view.bounds.size;
  46. tempSize.width = 130;
  47. CGSize size = [self.tableView sizeThatFits:tempSize];
  48. //sizeThatFits返回的是最合适的尺寸,但不会改变控件的大小
  49. return size;
  50. }else {
  51. return [super preferredContentSize];
  52. }
  53. }
  54. - (void)setPreferredContentSize:(CGSize)preferredContentSize{
  55. super.preferredContentSize = preferredContentSize;
  56. }
  57. - (void)didReceiveMemoryWarning {
  58. [super didReceiveMemoryWarning];
  59. // Dispose of any resources that can be recreated.
  60. }
  61. /*
  62. #pragma mark - Navigation
  63. // In a storyboard-based application, you will often want to do a little preparation before navigation
  64. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  65. // Get the new view controller using [segue destinationViewController].
  66. // Pass the selected object to the new view controller.
  67. }
  68. */
  69. @end