Jul. 14th, 2024
2024年 6月 9日

Post: iOS 入门 010: Xib

iOS 入门 010: Xib

Published 12:04 Apr 11, 2012.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content


  • 从文件中读取需要的组件

    • NSArray *bundelArr = [[NSBundle mainBundle]loadNibNamed:@"myCell" owner:nil options:nil]; cell = bundelArr[0]

    • nib其实是一个xib,里面是数组保存了各个组件。

  • 注册nib文件,并且给表格注册可重用的nib

    • 这样做的好处是能简化cell优化的过程,这样在初始化cell的时候就不用判断是否为nil


- (void)viewDidLoad {
    [super viewDidLoad];
    UINib *nib = [UINib nibWithNibName:@"myCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"myCell"];
  • 在对按钮进行设置target时候,注意判断是否一直在给button加监听时间


UIButton *button = (UIButton *)[cell viewWithTag:103];
// 在为按钮添加监听方法前,需要判断按钮是否已经被监听
if ([button allTargets].count == 0) {
    [button addTarget:self action:@selector(buy:forEvent:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"监听数量:%d", button.allTargets.count);
Pinned Message
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF