UITableView布局已损坏

时间:2015-07-26 12:50:37

标签: ios objective-c uitableview layout uinavigationcontroller

我是iOS开发的新手,现在主要使用UINavigationController和UITableViewController在iOS应用上工作。

enter image description here

我试图解决的问题是根viewController上的UITableView(自定义tableView单元格)布局破坏错误。当我从子viewController返回到根viewController时会发生这种情况。(左:原始,右:错误的布局)看起来UITableView位置由于某种原因被移动了,但我不确定为什么。< / p>

在MainViewController.m中

- (void)viewDidLoad {
  [super viewDidLoad];

  viewFrame = self.view.bounds;
  [self loadHeader];
  [self loadTable];
  self.view.backgroundColor = [UIColor whiteColor];

  numberOfCellRow = 20;
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:YES];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)loadView {
  [super loadView];

}

- (void)loadHeader {
  CGRect headerFrame = viewFrame;
  headerFrame.size.width = self.view.frame.size.width;
  headerFrame.size.height = 200;
  self.headerView = [[HeaderView alloc] initWithFrame:CGRectIntegral(headerFrame)];
  [self.view addSubview:self.headerView];
  NSLog(@"header loaded");
}

- (void)loadTable {
  CGRect tableFrame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y + 150,
                                 viewFrame.size.width, viewFrame.size.height-150);

  self.customTableView = [[UITableView alloc] initWithFrame:CGRectIntegral(tableFrame)];
  self.customTableView.backgroundColor = [UIColor clearColor];

  //set delegates
  self.customTableView.delegate = self;
  self.customTableView.dataSource = self;

  //set custom cell
  UINib *nib = [UINib nibWithNibName:@"CardViewCell" bundle:nil];
  [self.customTableView registerNib:nib forCellReuseIdentifier:@"Cell"];

  self.customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  [self.view addSubview:self.customTableView];

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  NSLog(@"cell loaded");
  dispatch_async(dispatch_get_main_queue(), ^{
    [self loadData:indexPath];

  });

}

- (void)loadData: (NSIndexPath *)indexPath {
  _tableCell.titleLabel.text = [[_articles objectAtIndex:indexPath.row] title];

  NSLog(@"%@", [[_articles objectAtIndex:indexPath.row] title]);

  [_tableCell setNeedsLayout];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  _tableCell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

  _tableCell.backgroundColor = [UIColor clearColor];
  [_tableCell.contentView setUserInteractionEnabled: NO];

  // add touch events
  [_tableCell.button addTarget:self
                        action:@selector(cellPressed:withEvent:)
              forControlEvents:UIControlEventTouchUpInside];

  NSLog(@"%@", NSStringFromCGRect(tableView.frame));

  return _tableCell;
}

在CardViewCell.m

- (void)drawRect:(CGRect)rect {
  //self.backgroundColor = [UIColor colorWithRed:228/255.0f green:228/255.0f blue:228/255.0f alpha:1.0f];
  NSLog(@"%f", rect.origin.x);
  self.backgroundColor = [UIColor clearColor];
  [self drawCardView];
  [self drawCardImage];
  [self drawButton];
  [self drawLabels];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];

  self.selectionStyle = UITableViewCellSelectionStyleNone;

}

- (void)drawCardImage {
  _cardViewBounds = _cardView.bounds;
  CGRect imageFrame = _cardViewBounds;
  imageFrame.size.height = _cardViewBounds.size.height/2+40;
  _cardImage = [[UIImageView alloc] initWithFrame:CGRectIntegral(imageFrame)];
  _cardImage.image = [UIImage imageNamed:@"sampleImage"];
  _cardImage.backgroundColor = [UIColor grayColor];
  _cardImage.clipsToBounds = YES;
  _cardImage.contentMode = UIViewContentModeScaleAspectFill;
  _cardImage.userInteractionEnabled = YES;
  _cardImage.tag = 100;

  UIBezierPath *maskPath;
  maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cardView.bounds
                                   byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                         cornerRadii:CGSizeMake(3.0, 3.0)];

  CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  maskLayer.frame = self.cardView.bounds;
  maskLayer.path = maskPath.CGPath;
  _cardImage.layer.mask = maskLayer;

  [self.cardView addSubview:_cardImage];
}

- (void)drawCardView {
  CGRect screen = [[UIScreen mainScreen] bounds];
  CGRect frame = CGRectMake(15, 25, (int)screen.size.width-30, 200);
  _cardView.layer.cornerRadius = 3;
  _cardView.backgroundColor = [UIColor greenColor];
  _cardImage.userInteractionEnabled = YES;
  _cardImage.tag = 101;
  _cardView.frame = CGRectIntegral(frame);

  CALayer *caLayer = _cardView.layer;
  caLayer.frame = _cardView.frame;
  caLayer.shadowRadius = 3.0f;
  caLayer.shadowOpacity = 0.4f;
  caLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
  caLayer.shouldRasterize = YES;
  // retina screen resolution
  [caLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
  [caLayer setShouldRasterize:YES];

  [self addSubview:_cardView];
}

- (void)drawButton {
  CGRect buttonSize = CGRectMake(_cardViewBounds.size.width-100, _cardViewBounds.size.height/2+10, 50, 50);
  _button = [[UIButton alloc] initWithFrame:CGRectIntegral(buttonSize)];
  _button.layer.cornerRadius = 25;
  _button.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
  _button.userInteractionEnabled = YES;
  _button.tag = 102;
  //_button.titleLabel.text = @"+";
  [_button setTitle:@"+" forState:UIControlStateNormal];
  [_button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:30.0]];

  CALayer *caButtonLayer = _button.layer;
  caButtonLayer.frame = _button.frame;
  caButtonLayer.shadowRadius = 3.0f;
  caButtonLayer.shadowOpacity = 0.2f;
  caButtonLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
  caButtonLayer.shouldRasterize = YES;
  // retina screen resolution
  [caButtonLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
  [caButtonLayer setShouldRasterize:YES];

  [self.cardView addSubview:_button];

}

- (void)drawLabels {
  CGRect titleLabelSize = CGRectMake( 10, _cardViewBounds.size.height-50, _cardViewBounds.size.width-20, 40);
  _titleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(titleLabelSize)];
  _titleLabel.backgroundColor = [UIColor clearColor];
  _titleLabel.numberOfLines = 2;
  _titleLabel.userInteractionEnabled = YES;
  _titleLabel.tag = 103;
  [_titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];

  [_cardView addSubview:_titleLabel];
  [_cardView sendSubviewToBack:_titleLabel];
}

- (void)prepareForReuse {
  [super prepareForReuse];

  for (UIView *subView in [self.contentView subviews]) {
    [subView removeFromSuperview];
  }
}

The code of this app is on this link.

有什么想法可以解决这个问题吗?

非常感谢您的想法和帮助。

1 个答案:

答案 0 :(得分:0)

视图错位的原因是_cardView.frame是CGRectZero消失后,我还没有找到解决方案。

您可以添加这些内容以查看更改。

 -(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(@"viewWillLayoutSubviews cardView %@",NSStringFromCGRect(self.tableCell.cardView.frame));
}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(@"viewDidLayoutSubviews cardView %@",NSStringFromCGRect(self.tableCell.cardView.frame));
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"viewWillDisappear cardView %@",NSStringFromCGRect(self.tableCell.cardView.frame));
}

-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"viewDidDisappear cardView %@",NSStringFromCGRect(self.tableCell.cardView.frame));
}

我认为您可以了解有关viewController和Cell的生命周期的更多信息。

LifeCycle

顺便说一下,当我看你的代码时,我不知道你是想用xib还是只用代码来编程。像下面的代码一样,这是来自代码的init,但你试图从xib获得。

只是我的意见。

- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {

    // make self from nib file
    UINib *nib = [UINib nibWithNibName:@"HeaderView" bundle:nil];
    self = [nib instantiateWithOwner:nil options:nil][0];
    self.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
    CGRect frame = CGRectZero;
    frame.size.width = [[UIScreen mainScreen] bounds].size.width;
    frame.size.height = 200;
    [self setFrame:frame];

}
return self;
}