【问题标题】:UITableViewCell frame.size.width is the same for ipad and iphone?UITableViewCell frame.size.width对于ipad和iphone是一样的吗?
【发布时间】:2013-04-14 03:41:55
【问题描述】:

试图在单元格的右下角放置一个包含日期的UILabel,无论它是在 iPhone 还是 iPad 上。

这是我目前拥有的,我需要做什么才能获得实际的单元格size.width,也尝试过cell.bounds.size.width,无论是 iPhone 还是 iPad,值都相同:

float xValue = cell.frame.size.width - 100;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 20.0f, 100, 17.0f)];
[nameLabel setTag:1];
[nameLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
nameLabel.textColor = [UIColor grayColor];
nameLabel.text = @"05-12-13";
// custom views should be added as subviews of the cell's contentView:
[cell.contentView addSubview:nameLabel];

ETA,这是整个自定义单元格类,也许有人可以看到导致标签重复的原因:

@interface VideoTableViewCell : UITableViewCell

+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView;
+ (CGFloat)heightForVideo:(id<VideoProtocol>)video;
- (void)updateCellForVideo:(id<VideoProtocol>)video;

@end

#define MARGIN 10

@implementation VideoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.textLabel.font  = [UIFont boldSystemFontOfSize:14];

        self.detailTextLabel.font = [UIFont systemFontOfSize:13];
        self.textLabel.numberOfLines = 2;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return self;
}

+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView {

    NSString *identifier = @"TweetCell";
    VideoTableViewCell *cell = (VideoTableViewCell *)[tableView
                                                    dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[VideoTableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:identifier];
    }

    return cell;
}

- (void)layoutSubviews {

    [super layoutSubviews];
    CGRect cvf = self.contentView.frame;
    self.imageView.frame = CGRectMake(0.0,
                                      0.0,
                                      cvf.size.height-1,
                                      cvf.size.height-1);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    CGRect frame = CGRectMake(cvf.size.height + MARGIN,
                              self.textLabel.frame.origin.y,
                              cvf.size.width - cvf.size.height - 2*MARGIN,
                              self.textLabel.frame.size.height);
    self.textLabel.frame = frame;

    frame = CGRectMake(cvf.size.height + MARGIN,
                       self.detailTextLabel.frame.origin.y,
                       cvf.size.width - cvf.size.height - 2*MARGIN,
                       self.detailTextLabel.frame.size.height);
    self.detailTextLabel.frame = frame;
}

- (void)setDateLabelWithDate:(NSDate *)date {


    float xValue = self.frame.size.width - 70;
    // create a custom label:                                        x       y   width  height
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 40.0f, 100, 12.0f)];
    dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
    [dateLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
    dateLabel.textColor = [UIColor grayColor];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd-yy"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    dateLabel.text = dateString;
    [self.contentView addSubview:dateLabel];
}

+ (CGFloat)heightForVideo:(id<VideoProtocol>)video {

    //create a dummy cell
    VideoTableViewCell *sampleCell  = [[VideoTableViewCell alloc]
                                         initWithStyle:UITableViewCellStyleSubtitle
                                         reuseIdentifier:nil];
    [sampleCell updateCellForVideo:video];

    //calculate the sizes of the text labels
    CGSize textSize = [video.title sizeWithFont: [VideoTableViewCell textLabelFont]
                                  constrainedToSize:sampleCell.textLabel.frame.size
                                      lineBreakMode:UILineBreakModeWordWrap];
    CGFloat minHeight = 51 + 10;  //image height + margin
    return MAX(textSize.height + 20, minHeight);    
}

+ (UIFont *)textLabelFont {
    return [UIFont systemFontOfSize:13];
}

- (void)updateCellForVideo:(id<VideoProtocol>)video {

    // set the text to the date with the tweet text
    self.textLabel.text = video.title;
    id<VideoAttrProtocol> speaker = [[video.speakers allObjects] objectAtIndex:0];
    self.detailTextLabel.text = speaker.name;
    [self setDateLabelWithDate:video.post_date];
    NSURL *url = [NSURL URLWithString:video.thumbnail];    
    [self.imageView setImageWithURL:url
                   placeholderImage:[UIImage imageNamed:@"Logo.png"]];
}


@end

【问题讨论】:

  • 您需要在视图控制器中显示tableView:cellForRowAtIndexPath: 方法。

标签: iphone ios ipad cocoa-touch uitableview


【解决方案1】:

您需要设置标签的 autoresizingMask。根据单元格的初始大小将标签放在正确的位置,无论它是什么。正确设置 autoresizingMask 后,标签会随着单元格大小的变化而调整。

在将标签添加到内容视图之前,添加:

// Keep the label in the bottom-right corner
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;

根据所有发布的代码进行更新:

在您的自定义单元格中,每次调用 setDataLabelWithDate: 方法时,您都会创建并添加一个标签。

您从updateCellForVideo: 调用此方法,也可能从您的视图控制器调用此方法(您没有显示该代码)。

您需要更新您的代码,以免每次调用 setDataLabelWithDate: 时都向单元格添加新标签。只添加一次标签。

【讨论】:

  • 试过了,它把标签放低了,然后当我开始滚动单元格时,标签在里面贴了两次,有些相互重叠。
  • 听起来你在一遍又一遍地添加标签。只需将其添加到单元格一次。
  • 我正在创建该 UILabel 并将其添加到每个已创建单元格的单元格中。也许是因为我正在使用 dequeueReusableCellWithIdentifier 进行单元重用模式?
  • 只有在对dequeueResuableCell...的调用返回nil时才将标签添加到单元格中。
  • @MarkW 请根据您的更新问题查看我的更新答案。
猜你喜欢
  • 2012-01-01
  • 2020-03-26
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
相关资源
最近更新 更多