gpt4 book ai didi

ios - 内容未显示在 UITableViewCell 上

转载 作者:行者123 更新时间:2023-11-29 01:44:52 25 4
gpt4 key购买 nike

我正在尝试使用 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];
});
});

最佳答案

tableview 的delegatedatasource 都设置好了吗?您可以通过设置来做到这一点:

self.tableView.delegate = self;
self.tableView.dataSource = self;

在您的 viewDidLoad 方法中。

您是否也在运行 numberOfSectionsInTableView:numberOfRowsInSection: 方法?

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

关于ios - 内容未显示在 UITableViewCell 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com