内容未在UITableViewCell上显示

时间:2015-08-16 23:10:23

标签: ios objective-c iphone uitableview cell

我正在尝试使用PureLayout创建一个自动调整大小的单元格,但即使所有JSON加载并正确分配给UITableViewCell对象,也不会显示任何数据。

以下是我尝试在自定义TableViewCell类中设置单元格的方法:

        self.backgroundColor = [UIColor whiteColor];
        self.accessoryType = UITableViewCellAccessoryNone;
        self.accessoryView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // Fix for contentView constraint warning
        [self.contentView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

        // Create ProfileImageView
        self.profilePicture = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.profilePicture.translatesAutoresizingMaskIntoConstraints = NO;
        self.profilePicture.contentMode = UIViewContentModeScaleAspectFill;
        self.profilePicture.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.profilePicture];

        // Name label
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.nameLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.numberOfLines = 1;
        [self.contentView addSubview:self.nameLabel];

        // Username label
        self.usernameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.usernameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.usernameLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
        self.usernameLabel.textColor = [UIColor darkGrayColor];
        self.usernameLabel.numberOfLines = 1;
        [self.contentView addSubview:self.usernameLabel];

        // Tweet label
        self.tweetLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.tweetLabel.userInteractionEnabled = YES;
        self.tweetLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.tweetLabel.numberOfLines = 0; // Must be set for multi-line label to work
        self.tweetLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.tweetLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
        self.tweetLabel.textColor = [UIColor darkGrayColor];
        [self.contentView addSubview:self.tweetLabel];

        // Favorite Button
        self.favoriteButton = [[DOFavoriteButton alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"star.png"] imageFrame:CGRectMake(0, 0, 15, 15)];
        [self.contentView addSubview:self.favoriteButton];

        // Retweet Button
        self.retweetButton = [[DOFavoriteButton alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"retweet.png"] imageFrame:CGRectMake(0, 0, 15, 15)];
        self.retweetButton.circleColor = [UIColor colorWithRed:0.04 green:0.83 blue:0.04 alpha:1.0];
        self.retweetButton.lineColor = [UIColor colorWithRed:0.08 green:0.93 blue:0.08 alpha:1.0];
        self.retweetButton.imageColorOn = [UIColor colorWithRed:0.26 green:0.96 blue:0.26 alpha:1.0];
        [self.contentView addSubview:self.retweetButton];

        // Reply Button
        self.replyButton = [[DOFavoriteButton alloc] initWithFrame:CGRectZero];
        [self.replyButton setBackgroundImage:[UIImage imageNamed:@"reply.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.replyButton];

        //
        //  CONSTRAIN
        //

        // Name Label
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:60];
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:35];
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:12];
        [self.nameLabel autoAlignAxis:ALAxisVertical toSameAxisOfView:self.contentView];

        // Username Label
        [self.usernameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameLabel withOffset:0.0];
        [self.usernameLabel autoSetDimension:ALDimensionHeight toSize:16.5];
        [self.usernameLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:58.0];

        // Profile Picture
        [self.profilePicture autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.nameLabel withOffset:-10.0];
        [self.profilePicture autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:12];
        [self.profilePicture autoSetDimension:ALDimensionHeight toSize:35];
        [self.profilePicture autoSetDimension:ALDimensionWidth toSize:35];

        // Tweet Label
        [self.tweetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.usernameLabel withOffset:8.0];
        [self.tweetLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:5.0];

以下是我在UITableViewController中启动单元格的方法:

TwitterCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TwitterCell"];

    // Add Tap Listeners
    UITapGestureRecognizer *nameLabelTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleCellNameTap:)];
    [cell.nameLabel addGestureRecognizer:nameLabelTap];

    UITapGestureRecognizer *profileImageTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleCellProfileImageTap:)];
    [cell.profilePicture addGestureRecognizer:profileImageTap];

    if (cell == nil) {
        cell = [[TwitterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TwitterCell"];
    }
    NSDictionary *data = tweets[indexPath.row];

    // NSDate
    NSString *nameString = data[@"user"][@"name"];
    NSString *screenName = data[@"user"][@"screen_name"];
    NSString *tweetString = data[@"text"];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dateFormatter setLocale:usLocale];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setDateFormat: @"EEE MMM dd HH:mm:ss Z yyyy"];

    NSDate *date = [dateFormatter dateFromString:[data objectForKey:@"created_at"]];
    NSDate *currentDateNTime        = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDateComponents *twitcomponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
    NSInteger twithour = [twitcomponents hour];
    NSInteger twitminute = [twitcomponents minute];
    NSInteger twitsecond = [twitcomponents second];
    NSDateComponents *realcomponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:currentDateNTime];
    NSInteger realhour = [realcomponents hour];
    NSInteger realminute = [realcomponents minute];
    NSInteger realsecond = [realcomponents second];

    NSInteger hour = realhour - twithour;
    NSInteger minute = realminute - twitminute;
    NSInteger second = realsecond - twitsecond;

    NSLog(@"Formatted hour: %ld, Formatted minute: %ld, Formatted second: %ld",(long)hour, (long)minute, (long)second);

    int adjustedSeconds = ((int)minute * 60) - abs((int)second);
    int adjustedMinutes = adjustedSeconds / 60;

    if (hour==1 > minute > 0) {
        int negmin = ((int)hour * 60) - abs((int)minute);
        int posmin = abs(negmin);
        NSString *strInt = [NSString stringWithFormat:@"%dm",posmin];
        cell.timeAgo.text = strInt;
    }else if (hour>0){
        NSString *strInt = [NSString stringWithFormat:@"%ldh",(long)hour];
        cell.timeAgo.text = strInt;
    }else if (hour==1){
            NSString *strInt = [NSString stringWithFormat:@"%ldh",(long)hour];
            cell.timeAgo.text = strInt;
    }else if(minute == 1 > second){
        NSString *strInt = [NSString stringWithFormat:@"%lds",(long)second];
        cell.timeAgo.text = strInt;
    }else{
        NSString *strFromInt = [NSString stringWithFormat:@"%dm",adjustedMinutes];
        cell.timeAgo.text = strFromInt;
    }

    [cell.favoriteButton addTarget:self action:@selector(favoriteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cell.retweetButton addTarget:self action:@selector(retweetButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    // Set Values
    [cell.nameLabel setText:nameString];
    cell.nameLabel.userInteractionEnabled = YES;
    cell.profilePicture.userInteractionEnabled = YES;
    cell.tweetLabel.text = tweetString;
    cell.usernameLabel.text = [NSString stringWithFormat:@"@%@",screenName];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *imageUrl = [[data objectForKey:@"user"] objectForKey:@"profile_image_url"];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.profilePicture.image = [UIImage imageWithData:data];
            CALayer *imageLayer = cell.profilePicture.layer;
            [imageLayer setCornerRadius:18];
            [imageLayer setMasksToBounds:YES];
        });
    });

1 个答案:

答案 0 :(得分:1)

tableview的delegatedatasource是否全部设定?您可以通过设置:

来实现
self.tableView.delegate = self;
self.tableView.dataSource = self;

viewDidLoad方法中。

您是否还运行numberOfSectionsInTableView:numberOfRowsInSection:方法?

我假设您正在初始化cellForRowAtIndexPath:中的单元格,并且您还记得在那里返回单元格吗?