如何在iOS上的一个tableview中放置两个不同的标签
我正在解析一个xml文件,并试图在表视图中显示xml文件的“标题”和“摘要”。我为每个“标题”创建一个子视图,为每个“摘要”创建一个子视图,并尝试将它们都放在单元格中。如果我只想写“标题”或“摘要”,但当我想写这两个词时就不行了。 这是我的代码:如何在iOS上的一个tableview中放置两个不同的标签,ios,label,tableview,cell,subview,Ios,Label,Tableview,Cell,Subview,我正在解析一个xml文件,并试图在表视图中显示xml文件的“标题”和“摘要”。我为每个“标题”创建一个子视图,为每个“摘要”创建一个子视图,并尝试将它们都放在单元格中。如果我只想写“标题”或“摘要”,但当我想写这两个词时就不行了。 这是我的代码: - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; UILa
- (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];
使用
只需补充一点,当我运行这段代码时,似乎每个单元格都有适合标题和摘要的正确高度,但是只有摘要出现在屏幕上,每个摘要后都有一个空格。好的,现在我的标题和摘要都出现在屏幕上了,但正如你在我的代码中看到的,我正试图在屏幕上打印整个标题和摘要,而不只是看到第一个单词和其他单词。。。现在我得到的是大单元格,里面有文本,比如:“这是Ti…”这是Summar…“我不想要。。。我需要打印完整的文本来定制单元格,请阅读也不要忘记接受并投票,如果这对你有帮助:)好的,但我认为你建议的方式是不同的,因为用这种方式你只能添加标题和摘要。之后,我想把日期或图像也,这就是为什么我这样做,并使自定义单元格适合我的文本。我已经阅读了本教程:它演示了如何使用动态高度。你认为你能在我使用的方法上帮助我吗?我想我真的很快就能做到这一点,因为如果我只想打印标题或摘要,它就可以了,但不能同时打印这两种内容……请检查这一点。它解释了如何向单元格中添加任何内容,并将其添加到xib文件中,这样您添加新内容和修改字体大小或位置就更容易了
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"] autorelease];
//Now you can use 2 different texts
cell.textLabel.text = @"Text1";
cell.detailTextLabel.text = @"Text2";