如何使用自定义UITableViewCell在单个单元格中放置三个标签?

时间:2011-07-26 17:31:13

标签: iphone objective-c uitableview

我正在自定义我的单元格并尝试在单个单元格中添加三个标签,一个在顶部,另外两个在底部单行。我正面临一些问题,包裹标签并整齐地显示。 这是代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CGSize titleSize = [[[self.titleArray objectAtIndex:indexPath.row] valueForKey:@"display_title"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                                                                 constrainedToSize:CGSizeMake(200, 9999999)
                                                                     lineBreakMode:UILineBreakModeWordWrap];

    CGSize detailSize = [@"APR 21, 2011" sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                                  constrainedToSize:CGSizeMake(200, 999999)
                                      lineBreakMode:UILineBreakModeWordWrap];

    CGSize thirdDetailSize = [@"This is the project type." sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                              constrainedToSize:CGSizeMake(200, 999999)
                                  lineBreakMode:UILineBreakModeWordWrap];

    return titleSize.height + MAX (detailSize.height, thirdDetailSize.height) + 15.0;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UILabel * mainLabel;
    UILabel * secondLabel;
    UILabel * thirdLabel;

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        NSArray *subviews = cell.contentView.subviews;
        for (UIView *vw in subviews)
        {
            if ([vw isKindOfClass:[UILabel class]])
            {
                [vw removeFromSuperview];
            }
        }

        mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,0,300,22)] ;
        mainLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
        mainLabel.numberOfLines = 20;
        mainLabel.lineBreakMode = UILineBreakModeWordWrap;
        mainLabel.backgroundColor = [UIColor clearColor];
        mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        mainLabel.adjustsFontSizeToFitWidth = YES;
        mainLabel.tag = kCellMainLabelTag;
        [cell.contentView addSubview:mainLabel];
        [mainLabel release];

        secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,30,140,22)];
        secondLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
        secondLabel.numberOfLines = 20;
        secondLabel.textColor = [UIColor grayColor];
        secondLabel.lineBreakMode = UILineBreakModeWordWrap;
        secondLabel.backgroundColor = [UIColor clearColor];
        secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        secondLabel.adjustsFontSizeToFitWidth = YES;
        secondLabel.tag = kCellSecondLabelTag;
        [cell.contentView addSubview:secondLabel];
        [secondLabel release];

        thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(145,30,140,22)];
        thirdLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
        thirdLabel.numberOfLines = 20;
        thirdLabel.textColor = [UIColor grayColor];
        thirdLabel.lineBreakMode = UILineBreakModeWordWrap;
        thirdLabel.backgroundColor = [UIColor clearColor];
        thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        thirdLabel.adjustsFontSizeToFitWidth = YES;
        thirdLabel.tag = kCellSecondLabelTag;
        [cell.contentView addSubview:thirdLabel];
        [thirdLabel release];
    }
    else
    {
        mainLabel = (UILabel *)[cell.contentView viewWithTag:kCellMainLabelTag];
        secondLabel = (UILabel *)[cell.contentView viewWithTag:kCellSecondLabelTag];
        thirdLabel = (UILabel *)[cell.contentView viewWithTag:kCellThirdLabelTag];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSString * date = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:@"docdt"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    NSDate * intime = [dateFormat dateFromString:date];
    NSString * userVisibleDateTimeString;
    if (intime != nil) {

        // Convert the NSDate to a user-visible date string.

        NSDateFormatter *  userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        assert(userVisibleDateFormatter != nil);

        [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];

        userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:intime];
    }

    NSString * signDate = userVisibleDateTimeString;
    NSArray * parts = [signDate componentsSeparatedByString:@"/"];
    NSString * exactFormat = [self displayDate:(NSArray *)parts];
    NSString * type = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:@"docty"];

    CGSize titleSize = [[[self.titleArray objectAtIndex:indexPath.row] valueForKey:@"display_title"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                                                                 constrainedToSize:CGSizeMake(280, 9999999)
                                                                     lineBreakMode:UILineBreakModeWordWrap];

    CGSize detailSize = [exactFormat sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                                  constrainedToSize:CGSizeMake(200, 999999)
                                      lineBreakMode:UILineBreakModeWordWrap];

    CGSize thirdDetailSize = [type sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
                                constrainedToSize:CGSizeMake(200, 999999)
                                    lineBreakMode:UILineBreakModeWordWrap];

    mainLabel.text = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:@"display_title"];
    mainLabel.frame = CGRectMake(mainLabel.frame.origin.x,
                                 mainLabel.frame.origin.y, titleSize.width, titleSize.height);
    secondLabel.text = exactFormat;
    secondLabel.frame = CGRectMake(secondLabel.frame.origin.x,
                                   mainLabel.frame.origin.y + titleSize.height, detailSize.width, detailSize.height);
    thirdLabel.text = type;
    thirdLabel.frame = CGRectMake(thirdLabel.frame.origin.x,
                                  mainLabel.frame.origin.y + titleSize.height, thirdDetailSize.width, thirdDetailSize.height);

return cell;
}

请帮助!!!

谢谢,

1 个答案:

答案 0 :(得分:2)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;} 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;}


enter code- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Create
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

}

// Configure
switch (indexPath.row) {
    case 0: 

    cell.textLabel.textColor=[UIColor whiteColor];
        CGRect myImageRec =CGRectMake(20,20, 50,21 );
        UILabel *lbl1=[[UILabel alloc]initWithFrame:myImageRec];
        lbl1.text=@"label1";
        [ cell addSubview: lbl1];
        CGRect frame1=CGRectMake(20,49, 50,21 );
        UILabel *lbl2=[[UILabel alloc]initWithFrame:frame1];
        lbl2.text=@"label2";
        [ cell addSubview: lbl2];
        CGRect frame2=CGRectMake(20,73, 50,21 );
        UILabel *lbl3=[[UILabel alloc]initWithFrame:frame2];
        lbl3.text=@"label3";
        [ cell addSubview: lbl3];
        break;

        default: break;
}
return cell;
} 

我使用了这个,我在一个单元格中显示三个标签,同时显示输出图像。 enter image description here