简体   繁体   English

如何将UIView初始化为UITableView的标头

[英]How to initialize a UIView as header of UITableView

I'm new to iOS programming and i tried to follow the example of building an UITableViewController in the book of BIG NERD RANCH IOS PROGRAMMING 4TH EDITION but with a storyboard instead of nibs. 我是iOS编程的新手,我尝试遵循在BIG NERD RANCH IOS PROGRAMMING 4TH EDITION一书中构建UITableViewController的示例,但使用的是故事板而不是笔尖。

everything works except that the UIView i added for the header does not appear. 一切正常,除了我为标题添加的UIView没有出现。

i added UIView to the UIViewTableController on top of the Prototype Cells in the storyboard. 我在情节提要的原型单元顶部将UIView添加到UIViewTableController中。 In the UIView, i had two buttons. 在UIView中,我有两个按钮。 Setting the UITableViewController as initial view controller, the storyboard appears as i designed in the storyboard. 将UITableViewController设置为初始视图控制器,情节提要会按照我在情节提要中的设计显示。 But when i use AppDelegate.m, adding lines of 但是当我使用AppDelegate.m时,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    TableTest *table = [[TableTest alloc] init];
    self.window.rootViewController = table;
    return YES;
}

The UIView doesn't appear. UIView没有出现。 I have assigned outlet for the UIView as headerView and tried 我已将UIView的插座分配为headerView并尝试

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

    UIView *header = self.headerView;
    return header;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40;

}

It shows a blank gray header so i suppose the UIView is not initialized... 它显示了一个空白的灰色标题,因此我想未初始化UIView ...

So I tried with my limited knowledge and a little search on the Internet by adding 所以我以有限的知识尝试了一下,并在Internet上做了一些搜索,添加了

(UIView *) headerView{
    if(!_headerView){
          _headerView = [_headerView init]
    }
    return _headerView
 }

still not working 还是行不通

How should I initialize it so it would appear when I use the AppDelegate? 我应该如何初始化它以便它在我使用AppDelegate时出现?

put this code in viewdidload method 将此代码放入viewdidload方法

self.tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 183.0f)];
        view.backgroundColor=[UIColor colorWithRed:0.376 green:0.086 blue:0.09 alpha:1];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 0, 24)];
        label.text = @"ffff";
        label.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        [label sizeToFit];
        label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;


        [view addSubview:label];
        view;
    });

It turns out I forgot to initialize the storyboard. 原来我忘了初始化情节提要。 well it's kinda stupid. 好吧,这有点愚蠢。 I assigned the storyboard ID and rewrote the init method by 我分配了情节提要ID,并重写了init方法,方法是

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
return [storyboard instantiateViewControllerWithIdentifier:@"TableTest"];

it works 有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM