我有以下代码:
do {
if ([[TBXML elementName:element] isEqualToString:@"wcqQuestionText"]) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
label.text = [TBXML textForElement:element];
[scrollView addSubview:label];
[label release];
[formulierText removeFromSuperview];
}
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
每次写入标签时,我都希望下一个标签位于刚刚创建的标签下。我该怎么做?
答案 0 :(得分:0)
创建浮点变量以跟踪标签框架的y位置,并在每次添加标签时增加它(如果所有标签都不适合其框架,您还需要调整滚动的contentSize):
static CGFloat y = 100.0f;
do {
if ([[TBXML elementName:element] isEqualToString:@"wcqQuestionText"]) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, y, 200, 100)];
label.text = [TBXML textForElement:element];
[scrollView addSubview:label];
[label release];
[formulierText removeFromSuperview];
y += 100.0f;
}
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
P.S。您是否考虑过使用标准的UITableView而不是自定义的UIScrollView?