viewForFooterInSection中的UILabel文本问题
我想在表格的两个不同页脚中显示两批文本。
第 3 部分的文本使用 titleForFooterInSection
:
- (NSString *)tableView:(UITableView *)tv titleForFooterInSection:(NSInteger)section
{
if (section == 3)
return @"SOME TEXT FOR SECTION 3";
else
return @"";
}
效果很好并且样式正确。 我还想添加一些格式更复杂的文本(实际上只是左对齐),因此我创建一个标签并将其添加到 viewForFooterInSection
中,该标签可以工作但没有填充(不是从 x = 20 开始) & y = 20,从主窗口边缘开始)并且样式与其他文本不同。
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
if (section == 1)
{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 50)] autorelease];
label.text = @"SOME COMPLICATED TEXT FOR SECTION 1\r\n"
"\r\n"
"MORE TEXT HERE\r\n"
"AND HERE.\r\n"
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.shadowColor = [UIColor colorWithWhite:0.8 alpha:0.8];
label.textColor = [UIColor blueColor];
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 0;
[label sizeToFit];
return label;
}
else
return nil;
}
-(CGFloat)tableView:(UITableView *)tv heightForFooterInSection:(NSInteger)section
{
if (section == 1)
return 180.0f;
else if (section == 3)
return 50.0f;
else
return 0.0f;
}
I want to display two lots of text in two different footers in my table.
The text for section 3 uses titleForFooterInSection
:
- (NSString *)tableView:(UITableView *)tv titleForFooterInSection:(NSInteger)section
{
if (section == 3)
return @"SOME TEXT FOR SECTION 3";
else
return @"";
}
and works great and is styled correctly.
I also want to add some more complicatedly formatted text (just left aligned really), so I create a label and add it to viewForFooterInSection
, which works but is not padded (doesn't start at x = 20 & y = 20, starts hard up against the main windows edge) and is not styled like the other text.
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
if (section == 1)
{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 50)] autorelease];
label.text = @"SOME COMPLICATED TEXT FOR SECTION 1\r\n"
"\r\n"
"MORE TEXT HERE\r\n"
"AND HERE.\r\n"
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.shadowColor = [UIColor colorWithWhite:0.8 alpha:0.8];
label.textColor = [UIColor blueColor];
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 0;
[label sizeToFit];
return label;
}
else
return nil;
}
-(CGFloat)tableView:(UITableView *)tv heightForFooterInSection:(NSInteger)section
{
if (section == 1)
return 180.0f;
else if (section == 3)
return 50.0f;
else
return 0.0f;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获得填充,请将标签添加到另一个视图中,然后将其作为页脚视图返回;
注意:您可能希望更好地限制
*footer
以避免内存泄漏或使用autorelease
。我不确定系统创建的标签的默认值是什么,该标签用于保存您作为页脚标题提供的文本。我想你在创建页脚标签时需要进行试验,因为谷歌没有提供任何有用的东西。
编辑:我没有将所有代码放在
viewForFooterInSection
方法中,而是使用自定义UIView;这允许我设置标题文本并根据内容调整标题大小。我在视图控制器中延迟加载它;
To get the padding add the label into another view before returning that as the footer view;
NB: You might want to scope
*footer
better to avoid a memory leak or useautorelease
.I'm not sure what the system defaults are for the label it creates to hold the text you supply as the title for footer. I guess you'll need to experiment when creating the footer label as Google hasn't thrown anything useful up.
EDIT: Rather than put all the code in the
viewForFooterInSection
method I use a custom UIView;This allows me to set my title text and have the header resize for the content. I lazy load this in my view controller;
确保您在
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
委托方法中返回正确的高度。Make sure u return proper height in
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
delegate method.