将 UILabel 添加到 UITableView - iphone
我是一个iOS开发新手。我有一个设置屏幕,它是 UITableView。我想添加一些解释。我正在使用以下代码来执行此操作,但它完全扭曲了文本。知道我做错了什么吗?
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 175)];
subjectLabel.font = [UIFont systemFontOfSize:16.0];
subjectLabel.numberOfLines = 0;
subjectLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
subjectLabel.backgroundColor = [UIColor clearColor];
//bodyLabel.textAlignment = UITextAlignmentLeft;
subjectLabel.text = @"mytext";
settingTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320, 370) style:UITableViewStyleGrouped] autorelease];
settingTableView.dataSource = self;
settingTableView.delegate = self;
[settingTableView addSubview:subjectLabel];
[self.view addSubview:settingTableView];
I am an iOS development newbie. I have a settings screen which is a UITableView. I want to add some explanation to it. I am using the following code to do it, but it skews up the text completely. Any idea what I am doing wrong?
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 175)];
subjectLabel.font = [UIFont systemFontOfSize:16.0];
subjectLabel.numberOfLines = 0;
subjectLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
subjectLabel.backgroundColor = [UIColor clearColor];
//bodyLabel.textAlignment = UITextAlignmentLeft;
subjectLabel.text = @"mytext";
settingTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320, 370) style:UITableViewStyleGrouped] autorelease];
settingTableView.dataSource = self;
settingTableView.delegate = self;
[settingTableView addSubview:subjectLabel];
[self.view addSubview:settingTableView];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
枕梦2024-12-04 13:00:19
tableViewHeader
是一个 UIView
,它被设置为 tableView 的 tableViewHeader
属性。如果您想在标题视图中使用 UILabel
,请创建一个单独的 UIView
(在代码中或在笔尖中),并将其设置为 tableView .tableHeaderView
属性。更多信息可以在这里找到: TableView 参考< /a>.希望有帮助!
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
建议1:您可以创建一个包含 UILabel 的单独视图,并将其放置在 UITableView 上方,并将 tableView y 位置放置在 UIView 的高度处。
备注:这很有用,因为当您滚动 tableView 时,默认标题将粘在顶部。
建议2:您可以使用 viewForHeaderInSection 委托方法。您可以在其中创建视图并添加 UILabel。 viewForHeaderInSection 返回 UIView,您可以返回包含 UILabel 的视图
备注:当您滚动 tableView 时,默认标题将随着 tableView 一起移动
Suggestion1 : You could have create a separate view which contains your UILabel and place above the UITableView and place your tableView y position would be from the height of the UIView.
Remark : This is useful because when you scroll the tableView the default header will be stick to top.
Suggestion2 : you can use viewForHeaderInSection delegate method. where you can create a view and add the UILabel. viewForHeaderInSection returns the UIView, which you can return your view which contains the UILabel
Remark : when you scroll the tableView the default header will move along with your tableView