使用笔尖作为带有笔尖类的表格部分标题 [英] Use a nib as a table section header with nib class

查看:25
本文介绍了使用笔尖作为带有笔尖类的表格部分标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个加载 nib 文件并将其设置为标题 UIView 的部分标题.这个笔尖文件也有一个关联的类,插座和动作连接到这个类,所以我想像往常一样用笔尖加载那个类.

I want to create a section header that loads a nib file and sets that as the header UIView. This nib file will also have an associated class where the outlets and actions are connected to so I want to load that class with the nib like normal.

我在网上搜索并找到了几个类似的答案,但我找不到任何适合我的答案.玩了几天后,我设法让视图正确显示,但尽管添加了连接并告诉文本以不同的方式显示,但它没有做任何事情.

I've scoured the web and found several similar answers but I can't get any to work for me. After playing around for a few days I managed to get the view to show correctly but it doesn't do anything despite adding connections and telling the text to show differently.

例如,它应该清除部分标题文本,如果它是用 nil 初始化的,并且它这样做了,但它仍然显示相同的文本,尝试更改它也不会反映,并且不会触发与按钮建立的任何连接.

For example it should clear the section title text if it's init with nil and it does that yet it still shows the same text, attempts to change it doesn't reflect either and any connections made with the button aren't triggered.

这是我的视图控制器

@interface ADUNewRowHeaderViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *sectionTitleField;
@property (strong, nonatomic) IBOutlet UIButton *addRowBtn;

@property(strong,nonatomic) NSString* sectionTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
                title:(NSString*) titleOrNil;

@end

这是实现

@implementation ADUNewRowHeaderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)titleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        if(titleOrNil != nil)
        {
            [self setSectionTitle:titleOrNil];
        }
        else
        {
            [self setSectionTitle:@""];
        }
    }
    return self;
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setSectionTitle:(NSString *)sectionTitle
{
    _sectionTitle = sectionTitle;
    [[self sectionTitleField] setText:sectionTitle];
}
@end

在实际的表视图控制器中它被列为

in the actual table view controller it's listed as

@property(nonatomic, strong) ADUNewRowHeaderViewController* secHeader;

并在viewDidLoad下的实现文件中

and in the implementation file under viewDidLoad as

[self setSecHeader:[[ADUNewRowHeaderViewController alloc] initWithNibName:nil bundle:nil title:nil]];
[[[self secHeader] addRowBtn] addTarget:self action:@selector(addNewRow:) forControlEvents:UIControlEventTouchUpInside];

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[self secHeader] view];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [[self secHeader] view].bounds.size.height;
}

这是动作的方法声明

- (IBAction)addNewRow:(id)sender;

推荐答案

你应该创建一个 UIView,而不是一个 UIViewController 子类..m 将包含:

You should make a UIView, instead of a UIViewController, subclass. The .m would contain:

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUp];
    }

    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setUp];
}

- (void)setUp
{    
    [[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:self options:nil];
    CGRect frame = self.frame;
    self.view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    [self addSubview:self.view];

    ... do other initialisations here ...
}

还有.h:

@interface ADUNewRowHeaderView : UIView

@property (nonatomic, strong) IBOutlet UIView*   view;

然后在 XIB 中:像往常一样,创建 ADUNewRowHeaderView 类的文件所有者.并将 XIB 的顶级视图的引用出口连接到上面的 view 属性(即在 File's Owner 中).

Then in the XIB: Make File's Owner of class ADUNewRowHeaderView, as usual. And connect the XIB's top-level View's Referencing Outlet to the view property above (i.e. in File's Owner).

然后你有一个 UIView 子类,你可以放在另一个 XIB 上(作为一个 UIView,你将类设置为 ADUNewRowHeaderView),或者在代码中实例化并添加为子视图.

You then have a UIView subclass you can place on another XIB (as a UIView which for which you set the Class to ADUNewRowHeaderView), or instantiate in code and add as subview.

(或者,您可以在代码中创建 UIView 及其子视图(按钮、标签等);那么您就不需要 XIB.但这当然只有在 UIView 简单且几乎没有自己的逻辑时才有效,以及一些易于在代码中布局的 UI 元素.)

(Alternatively you could create the UIView and it's subviews (buttons, labels, ...) in code; then you would not need a XIB. But this only works of course if the UIView is simple and has little own logic, and few UI elements that are easy to layout in code.)

这篇关于使用笔尖作为带有笔尖类的表格部分标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆