你的位置:首页 > 软件开发 > 操作系统 > iOS Programming Subclassing UITableViewCell

iOS Programming Subclassing UITableViewCell

发布时间:2015-05-20 19:00:50
iOS Programming Subclassing UITableViewCell 1.Creating BNRItemCell UITableViewCell is a UIView subclass.UITableViewCell是UIView的子类。Whe ...

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

iOS Programming Subclassing UITableViewCell

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重新调整他们的来腾出空间为编辑控制。

iOS Programming Subclassing UITableViewCell

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 iOS Programming Subclassing UITableViewCelltab). Change the Class to BNRItemCell

iOS Programming Subclassing UITableViewCell

iOS Programming Subclassing UITableViewCell

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

 

iOS Programming Subclassing UITableViewCell

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

{

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:iOS Programming Subclassing UITableViewCell

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。