您的位置:首页 > 产品设计 > UI/UE

iOS:UITableViewCell 方法 属性 详解

2014-07-31 22:02 387 查看
--参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html <UITableViewCell Class Reference>

UITableView中显示的每一个单元都是一个UITableViewCell对象,其初始化函数initWithStyle:reuseIdentifier:比较特别。这个主要是为了效率考虑,因为在tableView快速滑动的滑动的过程中,频繁的alloc对象是比较费时的,于是引入了cell的重用机制,这个也是我们在dataSource中要重点注意的地方。

========================UITableViewCell的风格========================

  我们可以通过cell的selectionStyle属性指定cell选中时的显示风格,以及通过accessoryType来指定cell右边的显示的内容,或者直接指定accessoryView来定制右边显示的view。cell的selectionStyle有四种风格: UITableViewCellStyleDefault、UITableViewCellStyleValue1、UITableViewCellStyleValue2、UITableViewCellStyleSubtitle;

1.UITableViewCellStyleDefault:

该格式提供了一个简单的左对齐黑色文本标签textLabel和一个可选的图像imageView。如果显示图像,那么图像将在最左边。这种格式虽然可以设置detailTextLabel,但是不会显示该标签。

2.UITableViewCellStyleValue1:

该格式居左显示黑色文本textLabel,居右显示较小字体蓝色文本detailTextLabel。经测试发现,它的样式跟Default样式差不多,左侧有图片,并且比Default样式右边多了个detailTextLabel

3.UITableViewCellStyleValue2:

该格式居左现实一个蓝色标签textLabel,在其右边左对齐显示一个小型黑色详细标签detailTextLabel。

4.UITableViewCellStyleSubtitle:

该格式与第一种相比,增加了对detailTextLabel的支持,该标签将会显示在textLabel标签的下面,较小的gray文本。

========================相关方法属性========================

******Managing the Predefined Content[b]******[/b]

--imageView属性:+ textLabel属性 + detailTextLabel属性:它们都是本身都是readonly,但并不是说界面就不可改变,它们的属性可以随意赋值。它们的具体位置参考上文UITableViewCell的style。

[b]******Accessing Views of the Cell Object******[/b]

--contentView属性:If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the celltransitions into and out of editing mode。位置参考下文"parts
of table-view cell"图片。

--backgroundView属性:The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for grouped-style tables UITableViewStyleGrouped). UITableViewCell adds the background view as a subview behind all other views
and uses its current frame location


--selectedBackgroundView属性:Default is nil for cells in UITableViewStylePlain, and non-nil for UITableViewStyleGrouped. The 'selectedBackgroundView' will be added as a subview directly above the backgroundView if not nil, or behind all other
views. It is added as a subview only when the cell is selected. Calling -setSelected:animated: will cause the 'selectedBackgroundView' to animate in and out with an alpha fade.

--multipleSelectionBackgroundView属性:If not nil, takes the place of the selectedBackgroundViewwhen using multiple selection.

******Managing Cell Selection and Highlighting******

--selectionStyle属性:The selection style is a backgroundView constant thatdetermines the color of a cell when it is selected.

--selected属性:The selection affects the appearance of labels, image, and background. When the selected state of a cell is set to YES, it draws the background for selected cells with its title in white. The default value is
NO. If you set the selection state to YES through this property, the transition to the new state appearance is not animated. For animated selected-state transitions, see the setSelected:animated: 方法。

--highlighted属性:The highlighting affects the appearance of labels, image, and background. When the highlighted state of a cell is set to YES, labels are drawn in their highlighted text color (default is white). The default
value is NO. If you set the highlighted state to YES through this property, the transition to the new state appearance is not animated. For animated highlighted-state transitions, see the setHighlighted:animated: method.

Note that for highlighting to work properly, you must fetch the cell’s labels using thetextLabel anddetailTextLabel properties and set each label’shighlightedTextColor property;
for images, get the cell’s image using theimageView property and set the UIImageView object’shighlightedImage property。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated方法; // animate between regular and selected state

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated方法; // animate between regular and highlighted state

******Managing Accessory Views******

--accessoryType属性:The type of standard accessory view the cell should use (normal state).The accessory view appears in the right side of the cell in the table view’s normal (default) state.The default isUITableViewCellAccessoryNone.
If a custom accessory view is set through the accessoryView property,the value of this property is ignored. If the cell is enabled and the accessory type isUITableViewCellAccessoryDetailDisclosureButton, the accessory
view tracks touches and, when tapped, sends thedata-source object atableView:accessoryButtonTappedForRowWithIndexPath: message。说明:Disclosure Indicator/Button是扩折箭头;DetailButton详情按钮;checkmark一个钩;前2者可以组合。

--accessoryView属性:A view that is used, typically as a control,on the right side of the cell (normal state)。if set, use custom view. ignore accessoryType.

--editingAccessoryType属性:跟上文accessoryType属性一样,只不过用在editing state。

--editingAccessoryView属性:跟上文accessoryView属性一样,只不过用在editing state。

备注:The accessory type cross-fades between normal and editing states if it set for both states; If this property is not set for both states, the cell is animated to slide or out, as necessary。

******Editing the Cell******

--editingStyle属性:it specifies whether the cell is in an editable state and, if it is, whether itshows an insertion or deletion control。The default value is UITableViewCellEditingStyleNone它是只读的,只能set
by UITableView using the delegate's value for cells who customize their appearance accordingly。

--editing属性indicates whether the cell is in an editable state。When a cell is in an editable state, it displays the editing controls specified for it。UseeditingStyle andshowsReorderControl
to specify these controls for the cell。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated方法:Toggles the receiver into and out of editing mode。并且当thesetEditing:animated: method of UITableView is invoked 相当This method is called on each visible cell。

--showingDeleteConfirmation属性:表示 whether the cell is currently showing the delete-confirmation button。

--showsReorderControl属性:determines whether the cell shows the reordering control。The default value is NO. If the value is YES , the reordering control temporarily replaces any accessory view。

For the reordering control to appear, you must not only set this property but implement theUITableViewDataSource methodtableView:moveRowAtIndexPath:toIndexPath:. In addition, if the data source
implementstableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row。

******Managing Content Indentation******

--indentationLevel属性:adjust content indent. default is 0.The amount of indentation is equal to theindentation level multiplied by the value in theindentationWidth property。即indentation
= indentationLevel * indentationWidth;

--indentationWidth属性:width for each level. default is 10.0

--shouldIndentWhileEditing属性:whether the cell background is indented when the table view is in editing mode.Thedefault value is YES.This property is unrelated to indentationLevel. Thedelegate
can override this value in tableView:shouldIndentWhileEditingRowAtIndexPath:. This property has an effect only on table views created in the grouped style (UITableViewStyleGrouped);

--separatorInset属性:The inset values for the cell’s content.You can use this property toadd space between the current cell’s contents and the left and right edges of the table.Only the left
and right inset values are respected;the top and bottom inset values are ignored.

******State Transitions & Subclass******

--prepareForReuse方法:Prepares a reusable cell for reuse by the table view'€™s delegate.If aUITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned
from theUITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection
state. The table view'€™s delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called.If you
override this method, you must be sure to invoke the superclass implementation.


- (void)willTransitionToState:(UITableViewCellStateMask)state方法:Called on the cell just before it transitions between cell states.

Subclasses of UITableViewCell can implement this method to animateadditional changes to a cell when it is changing state.UITableViewCell calls this methodwhenever a cell
transitions between states
, such as from a normal state (the default) to editing mode. The custom cell can set up and position any new views that appear with the new state. The cell then receives alayoutSubviews message (UIView)
in which it can position these new views in their final locations for the new state. Subclasses must always call super when overriding this method.

Note that when the user swipes a cell to delete it, the cell transitions to the state identified by theUITableViewCellStateShowingDeleteConfirmationMask constant but theUITableViewCellStateShowingEditControlMask
is not set.

- (void)didTransitionToState:(UITableViewCellStateMask)state方法:同上面方法,只不过时间是just after it transitions。

--备注:

1.tableviewCell里某设置可能不起作用,因为它可能被tableview的datasource/delegate里的方法覆盖/影响了。

========================说明========================

1.cell的不同状态





通过观察上面两幅图片我们可以看出来,当cell在进入编辑状态的时候,contentView会自动的缩放来给Editing control腾出位置。这也就是说如果我们把subView添加到contentView上,如果设置autoresizingMask为更具父view自动缩放的话,cell默认的机制会帮我们处理进入编辑状态的情况。而且在tableView是Grouped样式的时候,会为cell设置一个背景色,如果我们直接添加在cell上面的话,就需要自己考虑到这个背景色的显示问题,如果添加到contentView上,则可以通过view的叠加帮助我们完成该任务。综上,subView最好还是添加到cell的contentView中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: