使用自动布局在tableview单元格上看不到自定义附件按钮

时间:2015-07-25 05:38:36

标签: ios swift

我正在UItableview单元格上创建自定义附件图像。当我运行应用程序时,这些附件图像被剪裁(见第二个屏幕截图)。当我滚动表格视图时,附件图像不再被剪裁(参见第一个屏幕截图)。

代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor darkGrayColor];
    MaintableView = [[UITableView alloc]init];
    MaintableView.translatesAutoresizingMaskIntoConstraints = NO;
    MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    MaintableView.dataSource=self;
    MaintableView.delegate=self;
    MaintableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [myview addSubview:MaintableView];

    NSDictionary * views = NSDictionaryOfVariableBindings(MaintableView);

    NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[MaintableView]-5-|" options:0 metrics:nil views:views];

    NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[MaintableView]-50-|"options:0 metrics:nil views:views];

    [myview addConstraints:horizentalConstraint];
    [myview addConstraints:verticalConstraint];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return Mainarray.count;
}

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

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

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

    UIView *sectionHeaderView = [[UIView alloc]init];
    sectionHeaderView.backgroundColor = [UIColor clearColor];
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [MaintableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

    [delegate processCompleted:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [MaintableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor blackColor];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *Cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (Cell == nil)
    {
        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = [bg colorWithHexString:@"27AE60"];
    Cell.selectedBackgroundView = bgview;

    accessoryView =  accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0.jpg"]];
    accessoryView.backgroundColor = [UIColor clearColor];
    accessoryView.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:accessoryView];

    NSDictionary * views = NSDictionaryOfVariableBindings(accessoryView);

    NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:Cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.f];
    [Cell.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
    [Cell.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
    [Cell.contentView addConstraint:constraint];

    NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[accessoryView]-10-|" options:0 metrics:nil views:views];

    [Cell.contentView addConstraints:horizentalConstraint];


    Cell.textLabel.text = [Mainarray objectAtIndex:indexPath.section];
    Cell.textLabel.textColor = [UIColor blackColor];

    UIFont *CiutadellaBold = [UIFont fontWithName:@"Bitter-Regular" size:17.0f];
    [Cell.textLabel setFont:CiutadellaBold];

    Cell.textLabel.numberOfLines = 2;
    Cell.backgroundColor = [bg colorWithHexString:@"EAECEE"];

    [Cell.layer setCornerRadius:7.0f];
    [Cell.layer setMasksToBounds:YES];

    return Cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

滚动后:表格视图辅助图片显示如下:

enter image description here

滚动前:显示不清晰:

enter image description here

1 个答案:

答案 0 :(得分:0)

您正遇到data movementlist.insert()之间的冲突。您应该使用middle自己的list,或者您应该创建标签和图片。

这是一个不需要所有这些约束的解决方案。我用你的代码测试了它;它有效:

Cell.textLabel

或者,请按照我的评论中的建议操作,并在故事板中创建自定义accessoryView。然后,您就可以控制UITableViewCellaccessoryView重叠。