2013-04-14 93 views
0

試圖將包含日期的UILabel放置在單元格的右下角,無論是在iPhone還是iPad上。UITableViewCell frame.size.width是一樣的iPad和iPhone?

這是我現在有什麼,我需要做的就是實際的電池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 
+0

您需要在視圖控制器中顯示'tableView:cellForRowAtIndexPath:'方法。 – rmaddy

回答

2

您需要設置標籤的autoresizingMask。根據單元格的初始大小將標籤放在正確的位置,不管它是什麼。隨着autoresizingMask的正確設置,標籤將隨着單元格大小的變化而調整。

只需添加標籤內容視圖之前,添加:

更新基於所有張貼代碼:

在您的自定義單元格,您可以創建和每次添加一個標籤調用方法setDataLabelWithDate:

您可以從updateCellForVideo:以及可能從您的視圖控制器調用此方法(您不顯示該代碼)。

您需要更新代碼,以便每次調用setDataLabelWithDate:時都不會向單元添加新標籤。只添加一次標籤。

+0

嘗試過這種方式,它將標籤放在較低的位置,然後當我開始滾動單元格時,標籤內部張貼了兩次,其中一些重疊。 –

+0

聽起來像你一遍又一遍地添加標籤。只需將其添加到單元格中即可。 – rmaddy

+0

我正在創建該UILabel,並將其添加到所創建的每個單元格的單元格中。也許這是因爲我通過使用dequeueReusableCellWithIdentifier來完成單元重用模式? –