IOS UITableViewCell通过Nib加载并复用View

为什么会想到使用nib


公司项目代码,很多UI都是通过手写代码实现的,一个简单的 由3个Label组成的 UITableViewCell也是通过手写代码,通过Android项目的经验,是否有和android中类似xml实现可复用UI的方法呢,查了google,答案是肯定的,用nib就可以实现

nib的好处

  1. 相对于手写UI更方便、快速,界面修改更迅速更直观,产生的bug也少
  2. 相对storyboard更轻量级,更灵活,针对性也更高,也更方便复用

例子:UITableViewCell通过Nib加载并复用View

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“example”];
 if (!cell) {
 	[tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"example"];
        cell = [tableView dequeueReusableCellWithIdentifier:DASCellIdentifier];
 }

然后用nib实现对应的MyCell即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章