Date
Sep. 8th, 2024
 
2024年 8月 6日

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

Xib

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

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

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

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

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

e.g.

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

e.g.

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
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系