表头没有显示在TableView中

时间:2015-05-11 02:58:27

标签: ios uitableview

我是否需要以某种方式声明标头,或者self.navigationItem.title是否应在表格视图中单独显示标题?

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.items = @[@{@"name" : @"Take out the trash", @"category" : @"Home"}, @{@"name" : @"Go Shopping", @"category" : @"Home"}].mutableCopy;

self.navigationItem.title = @"What needs to be done";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)];
}

4 个答案:

答案 0 :(得分:0)

我是通过以下方式完成的:

在storyboard / xib中使用UIView作为标题。 然后在你的.h文件中,

IBOutlet UIView *headerView;

然后在你的.m文件中,

yourTable.tableHeaderView = headerView;

希望有所帮助。

答案 1 :(得分:0)

设置navigationItem.title仅在视图控制器嵌入UINavigationController时才有效。

在屏幕上显示此视图控制器之前调用此方法,将视图控制器包装在导航控制器中并显示生成的vc:

[UINavigationController initWithRootViewController:myViewController]

答案 2 :(得分:0)

这取决于你

如果你想将标题设置为整个页面的直接字符串,你可以使用

self.title = @"your title";

但是如果您想要自己的标签或图像作为titleView,您可以创建自定义视图(UILabel,UIButton,UIImageView,UIView和So On ......)并设置如下

self.navigationItem.titleView = yourCustomViewObject

如果你想在tableView上使用标题,那么你应该这样做 在viewDidLoad中创建自定义视图并设置self.tableView.tableHeaderView = yourCustomViewObject,或者您可以在Hiren回答时使用xib。

如果您有部分,那么您也可以使用tableView的代表之一,即

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

一起
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

这会对你有帮助。

答案 3 :(得分:0)

UITableView具有tableHeaderView属性。

您只需创建一个新的UIView,在其中添加UILabel,并使用您的新UIView设置tableHeaderView。

-(void)viewDidLoad
{
    //your viewDidLoad code
    // ...

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XPOSITION, YPOSITION, WIDTH, HEIGHT)];
    UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XPOSITION, YPOSITION, WIDTH, HEIGHT)];
    labelView.text = @"What needs to be done";
    [headerView addSubview:labelView];
    self.tableView.tableHeaderView = headerView;
}
相关问题