使用笔尖作为表部分标题与笔尖类

问题描述:

我想创建一个部分标题加载一个笔尖文件并将其设置为标题UIView。这个nib文件还会有一个关联的类,其中的出口和动作是连接的,所以我想像正常一样用nib加载那个类。使用笔尖作为表部分标题与笔尖类

我搜遍了网页,发现了几个类似的答案,但我无法为我工作。在玩了几天之后,我设法让视图正确显示,但是尽管添加了连接并告诉文本以不同的方式显示,但它并没有做任何事情。

例如,它应该清除节标题文本,如果它是init的nil,它会这样做,但它仍然显示相同的文本,尝试更改它并不反映任何与按钮的连接都不会触发。

这里是我的观点

@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 

视图控制器,这里是它的上市为

@property(nonatomic, strong) ADUNewRowHeaderViewController* secHeader; 

,并在实际的表视图控制器的实现

@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 

viewDidLoad下的实现文件为

[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将包含:

- (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; 

然后在厦门国际银行:请ADUNewRowHeaderView类的文件的所有者,像往常一样。并将XIB的*View的Referencing插座连接到上面的view属性(即在文件所有者中)。

然后,您可以在另一个XIB上放置一个UIView子类(作为您将其设置为ADUNewRowHeaderView的类的UIView),或者在代码中实例化并添加为子视图。 (或者你可以在代码中创建UIView及其子视图(按钮,标签......);那么你不需要XIB,但是这只适用于当然如果UIView很简单并且没有自己的逻辑,以及很少的易于在代码中布局的UI元素。)

+0

除了我必须删除'CGRect frame = self.frame; self.view.frame = CGRectMake(0,0,frame.size.width,frame.size.height);'从setUp和它的工作后 – 2013-04-27 21:48:43

+0

也UIButton我有在笔尖反应明显慢于它的工具栏最初是在,像我可以在几秒钟内按下工具栏按钮,它会immikidiately添加这些行,但如果我在一秒钟内按下新的笔尖按钮多次,它可能只能添加一个或两个。该按钮正常响应,它只是行不会立即添加(这不是一个问题,只是我注意到) – 2013-04-27 21:52:03

+0

请选择此为答案。 – 2013-04-27 22:04:03