为什么我的tableView伸展了?

时间:2017-04-24 10:09:51

标签: ios uitableview xib

我创建了一个LMLUpspringChoosePeriodView,并在其上有一个tableView和一个backgroundView, 我可以在我的项目中使用LMLUpspringChoosePeriodView作为弹出视图。

这是它的目录:

LMLUpspringChoosePeriodView有ChoosePeriodTableViewHeader和LMLUpspringPeriodCell,所有这些我都创建了一个XIB:

在LMLUpspringPeriodCell中,我将约束添加到名称标签中,因此它必须位于LMLUpspringPeriodCell的中心:

enter image description here

但是,当我将LMLUpspringChoosePeriodView显示为弹出视图时,出现了一个问题:

tableView似乎拉伸了屏幕。

我的tableView的约束是这样的:

enter image description here

我的代码如下:

LMLUpspringChoosePeriodView.h:

typedef void(^LMLUpspringBlock)();

#import <UIKit/UIKit.h>

@interface LMLUpspringChoosePeriodView : UIView
@property (weak, nonatomic) IBOutlet UIView *upspringBackView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom_tableView;

@property (nonatomic, copy) LMLUpspringBlock block;

- (void)showSelf;
- (void)hideSelf;

@end

LMLUpspringChoosePeriodView.m:

#import "LMLUpspringChoosePeriodView.h"
#import "LMLUpspringPeriodCell.h"
#import "ChoosePeriodTableViewHeader.h"

@interface LMLUpspringChoosePeriodView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width_tableView;

@property (nonatomic, strong) NSArray *title_arr;

@end

@implementation LMLUpspringChoosePeriodView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib {

    [super awakeFromNib];
    //_width_tableView.constant = KWidth;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    ChoosePeriodTableViewHeader *header = [[NSBundle mainBundle] loadNibNamed:@"ChoosePeriodTableViewHeader" owner:self options:nil].firstObject;

    header.cancel_block = ^() {

        [self hideSelf];
    };

    return header;
}

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


    LMLUpspringPeriodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LMLUpspringPeriodCell"];
    if (cell == nil) {

        cell = [[NSBundle mainBundle] loadNibNamed:@"LMLUpspringPeriodCell" owner:self options:nil].firstObject;
    }

    // 配置cell
    cell.title_label.text = self.title_arr[indexPath.row];

    if (indexPath.row == 3) {

        cell.bottom_line.hidden = YES;
    }

    [self setNeedsLayout];
    [self layoutIfNeeded];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 48;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    LMLUpspringPeriodCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSString *period_str = cell.title_label.text;

    self.block(period_str);

    [self hideSelf];
}

#pragma mark - action

- (void)showSelf {

    self.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{

        _bottom_tableView.constant = -49;
        _upspringBackView.alpha = 0.3f;
    }];

}

- (void)hideSelf {


    _bottom_tableView.constant = -_tableView.bounds.size.height - 49;

    [UIView animateWithDuration:0.25 animations:^{

        _upspringBackView.alpha = 0.f;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {

        if (finished) {

            self.hidden = YES;
        }
    }];

}

- (IBAction)tapBackView:(id)sender {

    [self hideSelf];
}

#pragma mark - setter

-(NSArray *)title_arr {


    if (!_title_arr) {

        _title_arr = @[@"当天", @"最近一周", @"最近一个月", @"最近三个月"];
    }

    return _title_arr;
}

@end

ChoosePeriodTableViewHeader.h:

#import <UIKit/UIKit.h>

typedef void(^CancelChooseUpspringView)();


@interface ChoosePeriodTableViewHeader : UIView

@property (nonatomic, copy) CancelChooseUpspringView cancel_block;


@end

ChoosePeriodTableViewHeader.m:

#import "ChoosePeriodTableViewHeader.h"

@implementation ChoosePeriodTableViewHeader

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

// 取消
- (IBAction)cancel:(UIButton *)sender {

    self.cancel_block();
}

LMLUpspringPeriodCell.h:

#import <UIKit/UIKit.h>

typedef void(^ConfirmChoosePeriod)(NSString *);

@interface LMLUpspringPeriodCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *title_label;
@property (weak, nonatomic) IBOutlet UIView *bottom_line;

@property (nonatomic, copy) ConfirmChoosePeriod confirm_block;

@end

LMLUpspringPeriodCell.m:

#import "LMLUpspringPeriodCell.h"

@implementation LMLUpspringPeriodCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

    // Configure the view for the selected state
}

@end

我尝试在awakeFromNib的{​​{1}}方法中使用以下代码(您可以看到LMLUpspringChoosePeriodView.m代码,awakeFromNib方法中有注释):

LMLUpspringChoosePeriodView.m

使用_width_tableView.constant = KWidth; 替换tableView以约束tableView,它仍然拉伸,但使用以下代码:

trailling

在iPhone7中它是完美的,但在iPhone7 Plus中有问题:

所以,这是一个非常奇怪的问题,怎么办呢?

还不行,有第二个问题,最后一项无法点击,我不知道是否与标签栏有关。

如何解决这两个问题?提前谢谢。

编辑-1

我将LMLUpspringChoosePeriodView添加到VC上的kwyWindow:

_width_tableView.constant = KWidth / 2; 

编辑-2

这是我的PurchaseRecordVC.m,在那里我展示了@property (nonatomic, strong) LMLUpspringChoosePeriodView *upspring_v; .... [[UIApplication sharedApplication].keyWindow addSubview:self.upspring_v];

LMLUpspringChoosePeriodView

1 个答案:

答案 0 :(得分:0)

我创建了一个满足相同条件的游乐场,但稍作修改后将其用作示例

你走了:

https://wetransfer.com/downloads/7cf7267e51b7d265204a3ceaed48d0fd20170424125153/c92374