簡體   English   中英

如何在iOS的一個表格視圖中放入2個不同的標簽

[英]How to fit 2 different labels in one tableview on iOS

我正在解析xml文件,並試圖在表視圖中顯示xml文件的“標題”和“摘要”。 我為每個“標題”創建一個子視圖,為每個“摘要”創建一個子視圖,並嘗試將它們都適合單元格。 如果我僅嘗試放入“標題”或“摘要”,而當我嘗試同時放入兩者時,則不起作用。 這是我的代碼:

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

UITableViewCell *cell;
UILabel *label=nil , *secondLabel = nil;

cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

    label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setMinimumFontSize:FONT_SIZE];
    [label setNumberOfLines:0];
    [label setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
    [label setTag:1];


    [[cell contentView] addSubview:label];

    secondLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [secondLabel setLineBreakMode:UILineBreakModeWordWrap];
    [secondLabel setMinimumFontSize:FONT_SIZE];
    [secondLabel setNumberOfLines:0];
    [secondLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    [secondLabel setTag:2]; 

    [[cell contentView] addSubview:secondLabel];    
}



int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
NSString *text1 = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];  
NSString *text2 = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];    

if (!label)
    label = (UILabel*)[cell viewWithTag:1];
if (!secondLabel)
    secondLabel = (UILabel*)[cell viewWithTag:2];    

[label setText:text1];
[secondLabel setText:text2];

[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size1.height, 44.0f))];
[secondLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size2.height, 44.0f))]; 

return cell;  }

和:

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

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *text1 = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *text2 = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];    


CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];    
CGFloat height = MAX(size1.height + size2.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2);}

謝謝!

代替

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

采用

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"] autorelease];


//Now you can use 2 different texts    
cell.textLabel.text = @"Text1";
cell.detailTextLabel.text = @"Text2";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM