iOS Programming Subclassing UITableViewCell
iOS Programming Subclassing UITableViewCell
1.Creating BNRItemCell
UITableViewCell is a UIView subclass.
UITableViewCell是UIView的子类。
When subclassing UIView (or any of its subclasses), you often override its drawRect: method to customize the view's appearance.
当继承UIView时,你经常需要重写drawRect方法来适应view的appearance。
However, when subclassing UITableViewCell, you usually customize its appearance by adding subviews to the cell.
当继承UITableViewCell时,你经常适应它的appearance 通过往cell 里添加subviews。
You do not add them directly to the cell though; instead you add them to the cell's content view.
你把他们添加到cell得content view中。
Each cell has a subview named contentView, which is a container for the view objects that make up the layout of a cell subclass

Adding subviews to the contentView instead of directly to the cell itself is important because the cell will resize its contentView at certain times.
把subview添加到content view上而不是直接添加到cell上很重要,因为cell 将resize 它的content view 在特定的时间。
For example, when a table view enters editing mode the contentView resizes itself to make room for the editing controls
当table view 进入editing 模式时,contentView重新调整他们的来腾出空间为编辑控制。

The cell cannot adjust its size when entering edit mode (it must remain the width of the table view), but the contentView can resize, and it does.
1.1
By the way, notice the UIScrollView in the cell hierarchy? That is how iOS moves the contents of the cell to the left when it enters editing mode. You can also use a right-to-left swipe on a cell to show the delete control, and this uses that same scroll view to get the job done. It makes sense then that the contentView is a subview of the scroll view.
注意在cell hierarchy中有一个UIScrollView?
这是为什么cell的content移动到了左边当进入编辑模式时。
Open Homepwner.xcodeproj. Create a new NSObject subclass and name it BNRItemCell. In BNRItemCell.h, change the superclass to UITableViewCell.
@interface BNRItemCell : UITableViewCell
2. Configuring a UITableViewCell subclass's interface
The easiest way to configure a UITableViewCell subclass is with a XIB file. Create a new Empty XIB
file and name this file BNRItemCell.xib. (The Device Family is irrelevant for this file.)
最简单的方式是用一个xib文件。
This file will contain a single instance of BNRItemCell. When the table view needs a new cell, it will
create one from this XIB file.
In BNRItemCell.xib, select BNRItemCell.xib and drag a UITableViewCell instance from the object library to the canvas.
Select the Table View Cell in the outline view and then the identity inspector (the
tab). Change the Class to BNRItemCell


Double-check that BNRItemCell.h looks like this: @interface BNRItemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *thumbnailView; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel; @property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@end

Note that you did not specify the File's Owner class or make any connections with it. This is a little different than your usual XIB files where all of the connections happen between the File's Owner and the archived objects.
你没有指明File' Owner 类和其他链接。这一点有一些不同。
3. Using BNRItemCell
In BNRItemsViewController's tableView:cellForRowAtIndexPath: method, you will create an
instance of BNRItemCell for every row in the table.
In BNRItemsViewController.m, import the header file for BNRItemCell so that
BNRItemsViewController knows about it.
#import "BNRItemCell.h"
Now that you are using a custom NIB file to load a UITableViewCell subclass, you will register that NIB instead.
现在你用一个通用的NIB文件加载UITableViewCell的子类,你将注册这个NIB。
In BNRItemsViewController.m, modify viewDidLoad to register BNRItemCell.xib for the "BNRItemCell" reuse identifier.
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the NIB file
UINib *nib = [UINib nibWithNibName:@"BNRItemCell" bundle:nil];
// Register this NIB, which contains the cell
[self.tableView registerNib:nib
forCellReuseIdentifier:@"BNRItemCell"];
}
The registration of a NIB for a table view is not anything fancy: the table view simply stores the UINib instance in an NSDictionary for the key "BNRItemCell".
一个NIB文件的注册一点也不神奇:table view 简单地存储这个UINib实例到一个NSDictionary,key是BNRItemCell。
A UINib contains all of the data stored in its XIB file, and when asked, can create new instances of the objects it contains.
一个UINib包含了所有存储在XIB文件中得数据,当被要求时,能创建它包含的新的对象实例。
Once a UINib has been registered with a table view, the table view can be asked to load the instance of BNRItemCell when given the reuse identifier "BNRItemCell".
一旦一个UINIb被registered 在一个table view,那么这个table view 就能被要求加载BNRItemCell的实例了。
In BNRItemsViewController.m, modify tableView:cellForRowAtIndexPath:.
// Get a new or recycled cell BNRItemCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"BNRItemCell" forIndexPath:indexPath];
// Configure the cell with the BNRItem
cell.nameLabel.text = item.itemName;
cell.serialNumberLabel.text = item.serialNumber;
cell.valueLabel.text =
[NSString stringWithFormat:@"$%d", item.valueInDollars];
BNRItemsViewController's table view will change its size to match the size of the window.
When a table view changes its width, each of its cells also change their width to match. Thus, you need to set up constraints in the cell that account for this change in width.
table view 改变它的宽度时,它的cell 将该改变他们的宽度来匹配。因此,如果你需要为宽度的变化设置限制。(cell 的高度不会改变,除非你明确指出。)
However, Auto Layout does not care about how big a view is when it is first created; it only cares about what the constraints say.
auto layout 并不关心你第一次创建的view有多大。它仅关心constraints 怎么说的。
If you were to add a constraint to the image view that pins its width to 500 points, the width would be 500 points – the original size does not factor in.
iOS Programming Subclassing UITableViewCell的更多相关文章
- iOS programming UITableView and UITableViewController
iOS programming UITableView and UITableViewController A UITableView displays a single column of dat ...
- ios Programming:The Big Nerd Ranch Guid(6th Edition) (Joe Conway & AARON HILLEGASS 著)
Introduction (已看) Prerequisites What Has Changed in the Sixth Edition? Our Teaching Philosophy How t ...
- iOS Programming UIStoryboard 故事板
iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...
- iOS Programming State Restoration 状态存储
iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...
- iOS Programming Web Services and UIWebView
iOS Programming Web Services and UIWebView The work is divided into two parts. The first is connecti ...
- Head First iOS Programming
内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...
- iOS Programming Recipe 6: Creating a custom UIView using a Nib
iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...
- iOS Programming Autorotation, Popover Controllers, and Modal View Controllers
iOS Programming Autorotation, Popover Controllers, and Modal View Controllers 自动旋转,Popover 控制器,Moda ...
- iOS Programming Controlling Animations 动画
iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...
随机推荐
- [IT新应用]无线投影技术
会议室内投影时,经常会有笔记本与投影仪之间因兼容性等无法切换的现象. 了解了下,无线投影方案的厂家大致如下: 1.http://www.taco.net.cn/ 2.巴可无线投影 https://ww ...
- 注入式开发(二):.NET 匿名函数
其实匿名函数就是个委托.只不过写起来更简洁. 为啥要用匿名函数呢?只是为了装逼吗? 诺诺诺 比如说,我们写代码,写着写着,发现有2个函数非常相像: string methodA(string data ...
- mongodb05---游标
游标cursor: 通俗的说,游标不是查询结果,而是查询的返回资源,或者接口. 通过这个接口,你可以逐条读取.就像php中的fopen打开文件,得到一个资源一样, 通过资源,可以一行一行的读文件. v ...
- C#使用 webBrowser 控件总结
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 进程、轻量级进程(LWP)、线程
进程.轻量级进程(LWP).线程 进程:程序执行体,有生命期,用来分配资源的实体 线程:分配CPU的实体. 用户空间实现,一个线程阻塞,所有都阻塞. 内核实现,不会所用相关线程都阻塞.用LWP实现,用 ...
- ubuntu 12.04 alt+tab无法切换窗口的问题(转载)
转自:http://www.2cto.com/os/201209/153282.html ubuntu 12.04 alt+tab无法切换窗口的问题 安装cpmpiz配置管理程序. sudo ...
- bzoj 3677: [Apio2014]连珠线【树形dp】
参考:http://www.cnblogs.com/mmlz/p/4456547.html 枚举根,然后做树形dp,设f[i][1]为i是蓝线中点(蓝线一定是父子孙三代),f[i][0]为不是,转移很 ...
- UOJ #206. 【APIO2016】Gap【交互题】
参考:https://blog.csdn.net/clover_hxy/article/details/70767653 人生第一次交互题...不是很难但是思维和传统题差别挺大的(以及并不会本地测试= ...
- P4475 巧克力王国(KDTree)
传送门 首先可以把约束条件看成一条直线,然后每个巧克力看成一个点,求给定区域内的点权和 用KDTree,每次判断一下当前矩形是否整个都在里面或都在外面,是的话直接返回,否则的话递归 注意,必须该矩形四 ...
- [App Store Connect帮助]八、维护您的 App(2)将 App 从 App Store 中移除
如果您不想继续向顾客提供您的 App,您可以将其从 App Store 中移除,这样会移除该 App 的所有版本.拥有该 App 先前版本的用户将无法更新 App,但只要您的合约有效,用户便仍可下载最 ...