UIKit--UITableView

来源:互联网 发布:python 频域分析 编辑:程序博客网 时间:2024/06/05 08:15

UITableView
Inherits : UIScrollView : UIView : UIResponder : NSObject
conforms to : UIFocusEnvironment, NSObject, UIAppearance, UIAppearanceContainer, NSCoding, UITraitEnvironment, UICoordinateSpace, UIDynamicItem
一个实例(或简单的表格,表格视图)是一个用于显示和编辑的分层列表信息。
一个表视图在一个列中显示项目列表。表格是UIScrollView的子类,它允许用户滚动表,虽然表格只允许纵向滚动。这些细胞包括表的每个项目UITableViewCell对象;UITableView使用这些对象绘制的表行可见。细胞有内容标题和图像,可以有,靠近右边缘,附件的意见。标准的附件视图是信息披露指标或细节披露按钮;前者导致在数据层次结构的下一个层次,而后者则导致了一个选定项目的详细视图。辅助视图也可以框架控件,如开关和滑块,也可以是自定义的视图。表格视图可以进入编辑模式,用户可以插入,删除,排序表中的行。

一个表视图是由零个或多个部分组成的,每个部分都有自己的行。在表视图中的索引号确定了部分,并在一个部分中确定了它们的索引号。任何部分都可以在前面的一节标题,并可以选择后跟一个部分脚注。
表格视图有两种款式,UITableViewStyleplain和UITableViewStyleGrouped。当你创建一个表格实例必须指定表的风格,这种风格是无法改变的。在平原的风格,部分的页眉和页脚浮动以上内容如果一个完整的部分是可见的。一个表视图可以有一个索引,显示在表右边的一个酒吧(例如,“A”通过“Z”)。你可以触摸一个特定的标签跳转到目标部分。表格视图的分组样式提供了一个默认的背景颜色和所有单元格的默认背景视图。背景视图为特定区域中的所有单元格提供了一个可视化分组。例如,一组可能是一个人的名字和标题,另一组的人使用的电话号码,和另一组电子邮件帐户等。查看分组表示例的设置应用程序。在分组样式表中的视图不能有索引。

许多方法采取NSIndexPath UITableView对象作为参数和返回值。NSIndexpath UITableView宣布,使您得到代表行的索引类别(行性)和部分指标(部分产权),并从一个给定的行的索引和部分指标构建指标的路径(indexpathforrow:第:法)。特别是在多个部分的表视图中,您必须在其索引号确定一行之前,对该节索引进行评估。
一个UITableView对象必须有一个对象,作为一个数据源和一个对象作为代表;通常这些对象是应用程序的代理或更频繁,一个自定义UITableViewController对象。数据源必须采用UITableViewDatasource协议和委托必须采用UITableViewdelegate协议。数据源提供的信息,表格需要构建表和管理数据模型时,一个表的行插入,删除或重新排序。委托管理的表行的配置和选择,排排序,突出,辅助视图,和编辑操作。
当发送一setediting:动画:消息(与第一个参数是),表格视图进入编辑模式,它显示了每个可见的行编辑或重新排列控件,根据每个相关的editingstyle UITableViewCell。点击插入或删除控制使数据源接收党commiteditingstyle:forrowatindexpath:消息。你犯了缺失或插入调用deleterowsatindexpaths:withrowanimation:或insertrowsatindexpaths:withrowanimation:,适当。在编辑模式下,如果表观细胞有其showsreordercontrol属性设置为“是”,数据源接收党moverowatindexpath:toindexpath:消息。数据源可以选择性地删除排序控制细胞通过实施党canmoverowatindexpath:。
表格表格视图细胞可见行缓存。你可以创建内容或行为特征,不同于默认单元格自定义UITableViewCell对象;仔细看看表格视图单元格的说明。
表格视图重写layoutSubviews方法使它调用reloadData只有当你创建一个新实例表格或当你分配一个新的数据源。重装的表观清除当前状态,包括当前的选择。然而,如果你调用reloadData显式调用,它清除了这个国家和随后的任何直接或间接调用重载layoutSubviews不触发。
有关基本观点的行为信息,看到iOS视图编程指南。
状态保存
在iOS 6之后,如果你对一个表格视图的restorationidentifier属性分配一个值,它试图保存当前选中的行和第一个可见的行。表的数据源可以采用uidatasourcemodelassociation协议,这一种方式来确定行的内容独立的行的位置表中提供。如果表中的数据源采用uidatasourcemodelassociation协议,数据源将咨询的保存状态转换指数路径顶部可见排在任何选定的单元格的标识符。在恢复过程中,数据源中将会将这些标识符返回的路径索引和重建的顶部可见排,重选的细胞。如果表中的数据源不落实uidatasourcemodelassociation协议,滚动条的位置将被保存和恢复直接将选定单元格的索引路径。
如何保护和恢复工作状态的更多信息,查看iOS应用程序编程指南。
有关外观和行为配置的更多信息,见表视图。

Initializing a UITableView Object
- initWithFrame:style:
Designated Initializer
Initializes and returns a table view object having the given frame and style.

DeclarationSWIFTinit(frame frame: CGRect,     style style: UITableViewStyle)OBJECTIVE-C- (instancetype)initWithFrame:(CGRect)frame                        style:(UITableViewStyle)style

Parameters
frame
A rectangle specifying the initial location and size of the table view in its superview’€™s coordinates. The frame of the table view changes as table cells are added and deleted.
style
A constant that specifies the style of the table view. See Table View Style for descriptions of valid constants.
Return Value
Returns an initialized UITableView object, or nil if the object could not be successfully initialized.

Discussion
You must specify the style of a table view when you create it and you cannot thereafter modify the style. If you initialize the table view with the UIView method initWithFrame:, the UITableViewStylePlain style is used as a default.

Availability
Available in iOS 2.0 and later.
Configuring a Table View
style
Property
Returns the style of the table view. (read-only)

Declaration
SWIFT
var style: UITableViewStyle { get }
OBJECTIVE-C
@property(nonatomic, readonly) UITableViewStyle style
Discussion
See Table View Style for descriptions of the constants used to specify table-view style.

Availability
Available in iOS 2.0 and later.
- numberOfRowsInSection:
Returns the number of rows (table cells) in a specified section.

Declaration
SWIFT
func numberOfRowsInSection(_ section: Int) -> Int
OBJECTIVE-C
- (NSInteger)numberOfRowsInSection:(NSInteger)section
Parameters
section
An index number that identifies a section of the table. Table views in a plain style have a section index of zero.
Return Value
The number of rows in the section.

Discussion
UITableView gets the value returned by this method from its data source and caches it.

Availability
Available in iOS 2.0 and later.
See Also
numberOfSections

numberOfSections
Property
The number of sections in the table view. (read-only)

Declaration
SWIFT
var numberOfSections: Int { get }
OBJECTIVE-C
@property(nonatomic, readonly) NSInteger numberOfSections
Discussion
UITableView gets the value in this property from its data source and caches it.

Availability
Available in iOS 2.0 and later.
See Also
– numberOfRowsInSection:

rowHeight
Property
The height of each row (that is, table cell) in the table view.

Declaration
SWIFT
var rowHeight: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat rowHeight
Discussion
Row height is expressed in points. You may set the row height for cells if the delegate doesn’t implement the tableView:heightForRowAtIndexPath: method. The default value of rowHeight is UITableViewAutomaticDimension. Note that if you create a self-sizing cell in Interface Builder, the default row height is changed to the value set in Interface Builder. To get the expected self-sizing behavior for a cell that you create in Interface Builder, you must explicitly set rowHeight equal to UITableViewAutomaticDimension in your code.

There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).

Availability
Available in iOS 2.0 and later.
separatorStyle
Property
The style for table cells used as separators.

Declaration
SWIFT
var separatorStyle: UITableViewCellSeparatorStyle
OBJECTIVE-C
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
Discussion
The value of this property is one of the separator-style constants described in UITableViewCell Class Reference. UITableView uses this property to set the separator style on the cell returned from the delegate in tableView:cellForRowAtIndexPath:.

Availability
Available in iOS 2.0 and later.
See Also
separatorColor

separatorColor
Property
The color of separator rows in the table view.

Declaration
SWIFT
var separatorColor: UIColor?
OBJECTIVE-C
@property(nonatomic, strong) UIColor *separatorColor
Discussion
The default color is gray.

Availability
Available in iOS 2.0 and later.
See Also
separatorStyle

separatorEffect
Property
The effect applied to table separators.

Declaration
SWIFT
@NSCopying var separatorEffect: UIVisualEffect?
OBJECTIVE-C
@property(nonatomic, copy) UIVisualEffect *separatorEffect
Availability
Available in iOS 8.0 and later.
backgroundView
Property
The background view of the table view.

Declaration
SWIFT
var backgroundView: UIView?
OBJECTIVE-C
@property(nonatomic, strong) UIView *backgroundView
Discussion
A table view’s background view is automatically resized to match the size of the table view. This view is placed as a subview of the table view behind all cells, header views, and footer views.

You must set this property to nil to set the background color of the table view.

Availability
Available in iOS 3.2 and later.
separatorInset
Property
Specifies the default inset of cell separators.

Declaration
SWIFT
var separatorInset: UIEdgeInsets
OBJECTIVE-C
@property(nonatomic) UIEdgeInsets separatorInset
Discussion
In iOS 7 and later, cell separators do not extend all the way to the edge of the table view. This property sets the default inset for all cells in the table, much as rowHeight sets the default height for cells. It is also used for managing the “extra” separators drawn at the bottom of plain style tables.

For example, to specify a table view where the default left separator inset is 3 points and the default right separator inset is 11, you would write:

tableView.separatorInset = UIEdgeInsetsMake(0, 3, 0, 11);
In a right-to-left user interface, an inset that you set using the separatorInset property automatically flips its left and right measurements.

Special Considerations
Only left and right insets are honored. In a right-to-left user interface, the inset measurements are automatically flipped.

Availability
Available in iOS 7.0 and later.
cellLayoutMarginsFollowReadableWidth
Property
A Boolean value that indicates whether the cell margins are derived from the width of the readable content guide.

Declaration
SWIFT
var cellLayoutMarginsFollowReadableWidth: Bool
OBJECTIVE-C
@property(nonatomic) BOOL cellLayoutMarginsFollowReadableWidth
Availability
Available in iOS 9.0 and later.
See Also
readableContentGuide

Creating Table View Cells
- registerNib:forCellReuseIdentifier:
Registers a nib object containing a cell with the table view under a specified identifier.

Declaration
SWIFT
func registerNib(_ nib: UINib?,
forCellReuseIdentifier identifier: String)
OBJECTIVE-C
- (void)registerNib:(UINib *)nib
forCellReuseIdentifier:(NSString *)identifier
Parameters
nib
A nib object that specifies the nib file to use to create the cell.
identifier
The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.
Discussion
Before dequeueing any cells, call this method or the registerClass:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.

Availability
Available in iOS 5.0 and later.
See Also
tableView:cellForRowAtIndexPath: (UITableViewDataSource)

  • registerClass:forCellReuseIdentifier:
    Registers a class for use in creating new table cells.

Declaration
SWIFT
func registerClass(_ cellClass: AnyClass?,
forCellReuseIdentifier identifier: String)
OBJECTIVE-C
- (void)registerClass:(Class)cellClass
forCellReuseIdentifier:(NSString *)identifier
Parameters
cellClass
The class of a cell that you want to use in the table.
identifier
The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.
Discussion
Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

Availability
Available in iOS 6.0 and later.
- dequeueReusableCellWithIdentifier:forIndexPath:
Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.

Declaration
SWIFT
func dequeueReusableCellWithIdentifier(_ identifier: String,
forIndexPath indexPath: NSIndexPath) -> UITableViewCell
OBJECTIVE-C
- (__kindofUITableViewCell )dequeueReusableCellWithIdentifier:(NSString )identifier
forIndexPath:(NSIndexPath *)indexPath
Parameters
identifier
A string identifying the cell object to be reused. This parameter must not be nil.
indexPath
The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cell’s position in the table view.
Return Value
A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.

Discussion
For performance reasons, a table view’€™s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available, or creates a new one based on the class or nib file you previously registered, and adds it to the table.

IMPORTANT
You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

Availability
Available in iOS 6.0 and later.
- dequeueReusableCellWithIdentifier:
Returns a reusable table-view cell object located by its identifier.

Declaration
SWIFT
func dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?
OBJECTIVE-C
- (__kindofUITableViewCell )dequeueReusableCellWithIdentifier:(NSString )identifier
Parameters
identifier
A string identifying the cell object to be reused. This parameter must not be nil.
Return Value
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.

Discussion
For performance reasons, a table view’€™s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

Availability
Available in iOS 2.0 and later.
Accessing Header and Footer Views
- registerNib:forHeaderFooterViewReuseIdentifier:
Registers a nib object containing a header or footer with the table view under a specified identifier.

Declaration
SWIFT
func registerNib(_ nib: UINib?,
forHeaderFooterViewReuseIdentifier identifier: String)
OBJECTIVE-C
- (void)registerNib:(UINib *)nib
forHeaderFooterViewReuseIdentifier:(NSString *)identifier
Parameters
nib
A nib object that specifies the nib file to use to create the header or footer view. This parameter cannot be nil.
identifier
The reuse identifier for the header or footer view. This parameter must not be nil and must not be an empty string.
Discussion
Before dequeueing any header or footer views, call this method or the registerClass:forHeaderFooterViewReuseIdentifier: method to tell the table view how to create new instances of your views. If a view of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new one automatically.

If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.

Availability
Available in iOS 6.0 and later.
See Also
– dequeueReusableHeaderFooterViewWithIdentifier:

  • registerClass:forHeaderFooterViewReuseIdentifier:
    Registers a class for use in creating new table header or footer views.

Declaration
SWIFT
func registerClass(_ aClass: AnyClass?,
forHeaderFooterViewReuseIdentifier identifier: String)
OBJECTIVE-C
- (void)registerClass:(Class)aClass
forHeaderFooterViewReuseIdentifier:(NSString *)identifier
Parameters
aClass
The class of a header or footer view that you want to use in the table.
identifier
The reuse identifier for the header or footer view. This parameter must not be nil and must not be an empty string.
Discussion
Before dequeueing any header or footer views, call this method or the registerNib:forHeaderFooterViewReuseIdentifier: method to tell the table view how to create new instances of your views. If a view of the specified type is not currently in a reuse queue, the table view uses the provided information to create a one automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the aClass parameter replaces the old entry. You may specify nil for aClass if you want to unregister the class from the specified reuse identifier.

Availability
Available in iOS 6.0 and later.
See Also
– dequeueReusableHeaderFooterViewWithIdentifier:

  • dequeueReusableHeaderFooterViewWithIdentifier:
    Returns a reusable header or footer view located by its identifier.

Declaration
SWIFT
func dequeueReusableHeaderFooterViewWithIdentifier(_ identifier: String) -> UITableViewHeaderFooterView?
OBJECTIVE-C
- (__kindofUITableViewHeaderFooterView )dequeueReusableHeaderFooterViewWithIdentifier:(NSString )identifier
Parameters
identifier
A string identifying the header or footer view to be reused. This parameter must not be nil.
Return Value
A UITableViewHeaderFooterView object with the associated identifier or nil if no such object exists in the reusable view queue.

Discussion
For performance reasons, a table view’s delegate should generally reuse UITableViewHeaderFooterView objects when it is asked to provide them. A table view maintains a queue or list of UITableViewHeaderFooterView objects that the table view’s delegate has marked for reuse. It marks a view for reuse by assigning it a reuse identifier when it creates it (that is, in the initWithReuseIdentifier: method of UITableViewHeaderFooterView).

You can use this method to access specific template header and footer views that you previously created. You can access a view’€™s reuse identifier through its reuseIdentifier property.

Availability
Available in iOS 6.0 and later.
tableHeaderView
Property
Returns an accessory view that is displayed above the table.

Declaration
SWIFT
var tableHeaderView: UIView?
OBJECTIVE-C
@property(nonatomic, strong) UIView *tableHeaderView
Discussion
The default value is nil. The table header view is different from a section header.

Availability
Available in iOS 2.0 and later.
See Also
sectionHeaderHeight

tableFooterView
Property
Returns an accessory view that is displayed below the table.

Declaration
SWIFT
var tableFooterView: UIView?
OBJECTIVE-C
@property(nonatomic, strong) UIView *tableFooterView
Discussion
The default value is nil. The table footer view is different from a section footer.

Availability
Available in iOS 2.0 and later.
See Also
sectionFooterHeight

sectionHeaderHeight
Property
The height of section headers in the table view.

Declaration
SWIFT
var sectionHeaderHeight: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat sectionHeaderHeight
Discussion
This nonnegative value is used only if the delegate doesn’t implement the tableView:heightForHeaderInSection: method.

Availability
Available in iOS 2.0 and later.
See Also
tableHeaderView

sectionFooterHeight
Property
The height of section footers in the table view.

Declaration
SWIFT
var sectionFooterHeight: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat sectionFooterHeight
Discussion
This nonnegative value is used only in section group tables and only if the delegate doesn’t implement the tableView:heightForFooterInSection: method.

Availability
Available in iOS 2.0 and later.
See Also
tableFooterView

  • headerViewForSection:
    Returns the header view associated with the specified section.

Declaration
SWIFT
func headerViewForSection(_ section: Int) -> UITableViewHeaderFooterView?
OBJECTIVE-C
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
Parameters
section
An index number that identifies a section of the table. Table views in a plain style have a section index of zero.
Return Value
The header view associated with the section, or nil if the section does not have a header view.

Availability
Available in iOS 6.0 and later.
- footerViewForSection:
Returns the footer view associated with the specified section.

Declaration
SWIFT
func footerViewForSection(_ section: Int) -> UITableViewHeaderFooterView?
OBJECTIVE-C
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section
Parameters
section
An index number that identifies a section of the table. Table views in a plain style have a section index of zero.
Return Value
The footer view associated with the section, or nil if the section does not have a footer view.

Availability
Available in iOS 6.0 and later.
Accessing Cells and Sections
- cellForRowAtIndexPath:
Returns the table cell at the specified index path.

Declaration
SWIFT
func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?
OBJECTIVE-C
- (__kindofUITableViewCell )cellForRowAtIndexPath:(NSIndexPath )indexPath
Parameters
indexPath
The index path locating the row in the table view.
Return Value
An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.

Availability
Available in iOS 2.0 and later.
See Also
– indexPathForCell:

  • indexPathForCell:
    Returns an index path representing the row and section of a given table-view cell.

Declaration
SWIFT
func indexPathForCell(_ cell: UITableViewCell) -> NSIndexPath?
OBJECTIVE-C
- (NSIndexPath )indexPathForCell:(UITableViewCell )cell
Parameters
cell
A cell object of the table view.
Return Value
An index path representing the row and section of the cell, or nil if the index path is invalid.

Availability
Available in iOS 2.0 and later.
See Also
– cellForRowAtIndexPath:

  • indexPathForRowAtPoint:
    Returns an index path identifying the row and section at the given point.

Declaration
SWIFT
func indexPathForRowAtPoint(_ point: CGPoint) -> NSIndexPath?
OBJECTIVE-C
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
Parameters
point
A point in the local coordinate system of the table view (the table view’€™s bounds).
Return Value
An index path representing the row and section associated with point, or nil if the point is out of the bounds of any row.

Availability
Available in iOS 2.0 and later.
See Also
– indexPathForCell:

  • indexPathsForRowsInRect:
    An array of index paths each representing a row enclosed by a given rectangle.

Declaration
SWIFT
func indexPathsForRowsInRect(_ rect: CGRect) -> [NSIndexPath]?
OBJECTIVE-C
- (NSArray

0 0
原创粉丝点击