开发者

Add UILabel to UITableView - iphone

开发者 https://www.devze.com 2023-03-25 06:48 出处:网络
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

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 开发者_Go百科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];


A tableViewHeader is a UIView which is set as the tableViewHeader property of a tableView. If you want to have a UILabel in a header view, make a separate UIView (either in code, or in a nib), and set it as the tableView.tableHeaderView property. More information can be found here: TableView Reference. Hope that helps!


create a view in your view controller and add your lable to that and bind it ...

IBOutlet UIView *headerView1;

and add this code

settingTableView.tableHeaderView = headerView1;


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

0

精彩评论

暂无评论...
验证码 换一张
取 消