2011-07-26 4 views
0

私は自分のセルをカスタマイズしており、1つのセルに3つのラベルを追加しようとしています。私はいくつかの問題に直面しているラベルをラップし、きれいに表示します。 コード:カスタムUITableViewCellを使用して1つのセルに3つのラベルを合わせるにはどうすればいいですか?

- (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; 
} 

助けてください!

おかげで、

答えて

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]; 
     [email protected]"label1"; 
     [ cell addSubview: lbl1]; 
     CGRect frame1=CGRectMake(20,49, 50,21); 
     UILabel *lbl2=[[UILabel alloc]initWithFrame:frame1]; 
     [email protected]"label2"; 
     [ cell addSubview: lbl2]; 
     CGRect frame2=CGRectMake(20,73, 50,21); 
     UILabel *lbl3=[[UILabel alloc]initWithFrame:frame2]; 
     [email protected]"label3"; 
     [ cell addSubview: lbl3]; 
     break; 

     default: break; 
} 
return cell; 
} 

は、私はこれを使用していると私は私は、出力の画像を表示し、一つのセルに3つのラベルを表示します。 enter image description here

+0

私はあなたが問題を注意深く読んでいないと思うので、私はカスタマイズせずに1行に2つのラベルを表示したいと思いました。 – Ashutosh

関連する問題