UITableViewCell w / PureLayout出现Lop-sided

时间:2016-06-05 21:50:08

标签: ios objective-c uitableview autolayout

我创建了一个显示来自Parse Server的数据的UITableViewCell,但是当我运行我的应用程序时,对象被绘制在最左边,而不是正确对齐。我无法弄清楚这是我的PureLayout代码的问题还是另一个愚蠢的错误。我的单元格显示正常的唯一方法是,如果我向下滚动到我的tableview中未显示的单元格,这是它们正常​​显示的位置。我提供了一些问题的图像以及一些代码。 enter image description here enter image description here

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"ProfileCell";
    PFObject *data = self.objects[indexPath.row];
    NSLog(@"Data: %@", data);

    ProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {


        cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier];
    }

    for(UIView *view in cell.contentView.subviews){
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }

    // Pull date from data
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    NSData *dat = data.createdAt;
    NSString *dateString = [dateFormatter stringFromDate:dat];
    NSDate *date = [dateFormatter dateFromString:dateString];

#pragma mark - Line
    UIImageView *profileCellLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profileCellLine"]];
    [profileCellLine setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:profileCellLine];

    [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeRight];

#pragma mark - Vertical Line
    UIImageView *vertCellLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"verticalLine"]];
    [cell.contentView addSubview:vertCellLine];

    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:100];

#pragma mark - Profile Pic
    PFUser *fromUser = [data objectForKey:@"fromUser"];
    [fromUser fetchIfNeeded];
    PFFile *thumbnail = [fromUser objectForKey:@"profilePicture"];
    NSData *imageData = [thumbnail getData];
    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *profilePic = [[UIImageView alloc] init];
    if (imageData == nil) {
        profilePic.image = [UIImage imageNamed:@"profilePic"];
    }else {
        profilePic.image = image;
        profilePic.layer.cornerRadius = 17.5;
        profilePic.clipsToBounds = YES;
    }
    [profilePic setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:profilePic];

    [profilePic autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:vertCellLine withOffset:5];
    [profilePic autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:4.0];
    [profilePic autoSetDimension:ALDimensionHeight toSize:35];
    [profilePic autoSetDimension:ALDimensionWidth toSize:34];

#pragma mark - Profile Pic
    PFUser *toUser = [data objectForKey:@"toUser"];
    [toUser fetchIfNeeded];
    PFFile *thumbnailTo = [toUser objectForKey:@"profilePicture"];
    NSData *imageDataTo = [thumbnailTo getData];
    UIImage *imageTo = [UIImage imageWithData:imageDataTo];
    UIImageView *toProfilePic = [[UIImageView alloc] init];
    if (imageDataTo == nil) {
        toProfilePic.image = [UIImage imageNamed:@"defaultProPic"];
        toProfilePic.layer.cornerRadius = 12.5;
        toProfilePic.clipsToBounds = YES;
    }else {
        toProfilePic.image = imageTo;
        toProfilePic.layer.cornerRadius = 12.5;
        toProfilePic.clipsToBounds = YES;
    }
    [toProfilePic setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:toProfilePic];

    [toProfilePic autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:vertCellLine withOffset:20];
    [toProfilePic autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:24];
    [toProfilePic autoSetDimension:ALDimensionHeight toSize:25];
    [toProfilePic autoSetDimension:ALDimensionWidth toSize:24];


#pragma mark - Title Label
    UILabel *titleLabel = [[UILabel alloc] init];
    [titleLabel setNeedsDisplay];
    [titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor treeColor];
    titleLabel.font = [UIFont fontWithName:@"OpenSans" size:14.185];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = [data objectForKey:@"volunteerTitle"];
    [titleLabel setNeedsDisplay];
    [cell.contentView addSubview:titleLabel];

    [titleLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:toProfilePic withOffset:6];
    [titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:6];

#pragma mark - Location Icon
    UIImageView *locationIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"locationIcon"]];
    [locationIcon setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:locationIcon];

    [locationIcon autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:toProfilePic withOffset:6];
    [locationIcon autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:titleLabel withOffset:3.5];

#pragma mark - Location Label
    UILabel *locationLabel = [[UILabel alloc] init];
    [locationLabel setNeedsDisplay];
    [locationLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.textColor = [UIColor colorWithRed:0.643 green:0.655 blue:0.667 alpha:1];
    locationLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:8.881];
    locationLabel.textAlignment = NSTextAlignmentCenter;
    locationLabel.text = [data objectForKey:@"location"];
    [locationLabel setNeedsDisplay];
    [cell.contentView addSubview:locationLabel];

    [locationLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:locationIcon withOffset:0];
    [locationLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:locationIcon withOffset:3];

#pragma mark - Detail Label
    UILabel *detailLabel = [[UILabel alloc] init];
    [detailLabel setNeedsDisplay];
    [detailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    detailLabel.backgroundColor = [UIColor clearColor];
    detailLabel.textColor = [UIColor colorWithRed:0.620 green:0.639 blue:0.659 alpha:1];
    detailLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:12];
    detailLabel.textAlignment = NSTextAlignmentLeft;
    detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    detailLabel.numberOfLines = 4;
    detailLabel.adjustsFontSizeToFitWidth = NO;
    detailLabel.text = [data objectForKey:@"volunteerDescription"];
    [detailLabel setNeedsDisplay];
    [cell.contentView addSubview:detailLabel];

    [detailLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:vertCellLine withOffset:5];
    [detailLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:toProfilePic withOffset:4];
    [detailLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:3];

#pragma mark - Time Label

    // Extract Time from Date
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    timeFormatter.dateFormat = @"h:mm a";
    NSString *timeString = [timeFormatter stringFromDate:date];

    // UI
    UILabel *timeLabel = [[UILabel alloc] init];
    [timeLabel setNeedsDisplay];
    [timeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor darkGrayColor];
    timeLabel.font = [UIFont fontWithName:@"OpenSans-Semibold" size:18];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = timeString;
    [timeLabel setNeedsDisplay];
    [cell.contentView addSubview:timeLabel];

    [timeLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:toProfilePic];
    [timeLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:8];

#pragma mark - Date Label
    // Extract Time from Date
    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    dateF.dateFormat = @"M/dd/yyyy";
    NSString *dateStr = [dateF stringFromDate:date];

    // UI
    UILabel *groupLabel = [[UILabel alloc] init];
    [groupLabel setNeedsDisplay];
    [groupLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    groupLabel.backgroundColor = [UIColor clearColor];
    groupLabel.textColor = [UIColor colorWithRed:0.620 green:0.639 blue:0.659 alpha:1];
    groupLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:8.881];
    groupLabel.textAlignment = NSTextAlignmentCenter;
    groupLabel.text = dateStr;
    [groupLabel setNeedsDisplay];
    [cell.contentView addSubview:groupLabel];

    [groupLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:timeLabel withOffset:-3];
    [groupLabel autoAlignAxis:ALAxisVertical toSameAxisOfView:timeLabel];

#pragma mark - Clock Icon
    UIImageView *clockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"clockIcon"]];
    [clockIcon setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:clockIcon];

    [clockIcon autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:groupLabel withOffset:5];
    [clockIcon autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:timeLabel];


#pragma mark - Hours Label
    UILabel *hoursLabel = [[UILabel alloc] init];
    [hoursLabel setNeedsDisplay];
    [hoursLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    hoursLabel.backgroundColor = [UIColor clearColor];
    hoursLabel.textColor = [UIColor colorWithRed:0.620 green:0.639 blue:0.659 alpha:1];
    hoursLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:8.881];
    hoursLabel.textAlignment = NSTextAlignmentCenter;
    hoursLabel.text = [NSString stringWithFormat:@"%@ Hrs",[[data objectForKey:@"volunteerHours"] stringValue]];
    [hoursLabel setNeedsDisplay];
    [cell.contentView addSubview:hoursLabel];

    [hoursLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:clockIcon withOffset:0];
    [hoursLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:clockIcon withOffset:3];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

  [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [profileCellLine autoPinEdgeToSuperviewEdge:ALEdgeRight];

#pragma mark - Vertical Line
    UIImageView *vertCellLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"verticalLine"]];
    [cell.contentView addSubview:vertCellLine];

    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [vertCellLine autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:100];

你设置了左上角和左上角等...所以视图知道它自身的高度,但宽度,视图如何知道宽度值以填充它在父视图中。