2016-06-05 2 views
0

Я создал UITableViewCell, который отображает данные с сервера анализа, однако, когда я запускаю свое приложение, объекты рисуются в левом левом углу, а не выравниваются правильно. Я не могу понять, если это проблема с моим кодом PureLayout или другой глупой ошибкой. Единственный способ, которым мои ячейки выглядят нормальными, - это прокрутить вниз до ячейки, которая не показана в моем представлении таблицы, вот где они выглядят нормально. Я представил некоторые изображения проблемы, а также некоторый код. enter image description here enter image description hereUITableViewCell w/PureLayout Появляется Lop-sided

Вот мой код:

- (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; 
} 

ответ

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]; 

Вы установка левого верхнего правого и левого верхнего дна и т.д. ... так что вид знают, как высота его самостоятельно, но как насчет ширины, представление не знало значение ширины, чтобы заполнить его внутри родительского вида.

+0

так что я должен изменить, чтобы исправить эту ошибку – Prad

+0

ваш код слишком длинный и трудно читаемый, но я забыл установить ширину для vertCellLine. Что-то вроде [vertCellLine autoSetDimension: ALDimensionWidth toSize: 1]; – Nick

Смежные вопросы