-
UITableViewScrollPosition:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition (UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
滑动到某一行,UITableViewScrollPosition代表想要这一行显示在当前TableView中的位置。
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
滑动到选择的行。与上面类似。UITableViewRowAnimation:修改TableView时的动画效果。
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);
关于NSIndexSet:
NSIndexSet是一个无符号整数的集合,集合内内容不可重复且不可变。用来作为一个索引的集合。
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
这里的indexPaths也就代表着需要改变的位置的indexPath的集合。
------------------UITableViewDelegate代理:
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPathNS_DEPRECATED_IOS(2_0,3_0);
这个代理方法用来设置某个indexPath的Cell右边的附加内容的类型。UITableViewCellAccessoryType有四种类型,
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn't track
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
这是一个回调函数,也就是说,当某个IndexPath的Cell右边附加内容类型为Button,并且被那个按钮点击的时刻就会调用这个方法。- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);
当某一行高亮的时刻回调
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);
当某一行失去高亮的时刻回调。
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
用户改变选择的行之前的回调方法:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
用户即将选择某一行,通常可以通过覆写这个方法阻止用户选择某一行。而使用tableView:didSelectRowAtIndexPath方法来对用户的选择行为进行处理。
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);
与上面的一致。
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
通常用来对用户的选择行为进行处理。
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);
取消选择某一行的行为的回调方法。// Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
用来设置Cell在编辑状态下的类型,我们用的一般都是Delete,其实总共有三种:none,delete和insert。
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);
设置删除按钮上面的内容。
// Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views.
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
在Group类型下,当某一行处于编辑状态时是否缩进,默认YES。// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
这两个方法在TableView发生变化的时刻自动调用(insert.delete.move都会触发)。/ Moving/reordering
移动或者是排序之后调用的方法,通常在这个方法中对数据进行修改。
// Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
// Indentation
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies
定义某一行的Cell的外观显示凹痕效果的程度。// Copy/Paste. All three methods must be implemented by the delegate.
复制粘贴行为触发的方法。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(5_0);
设为YES的话,那么这个Cell中能够使用系统自带的复制粘贴行为。
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)senderNS_AVAILABLE_IOS(5_0);
决定在某一行上面某个行为能否执行,例如可以设置只执行系统的Copy方法,而屏蔽掉其他方法。
参考:http://blog.csdn.net/piziliweiguang/article/details/8281007
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)senderNS_AVAILABLE_IOS(5_0);
怎么定义执行某个行为,如怎么定义在某个Cell上面系统的复制行为代表复制什么内容。
if
(action == @selector(copy:)) {
15
[UIPasteboard generalPasteboard].string = [data objectAtIndex:indexPath.row];
16
}
关于UITableView的属性:
@property(nonatomic) CGFloat rowHeight; // will return the default value if unset
可以直接通过这个属性设置所有Cell的高度,而不需要通过代理方法。
@property(nonatomic) CGFloat sectionHeaderHeight; // will return the default value if unset
@property(nonatomic) CGFloat sectionFooterHeight; // will return the default value if unset
@property(nonatomic,readwrite,retain) UIView *backgroundViewNS_AVAILABLE_IOS(3_2);// the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices.
设置整个TableView的背景,不会随着TableView滑动而运动。- (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks
重新加载所有数据。也就是重新显示一次,跟初始化一样。
- (void)reloadSectionIndexTitlesNS_AVAILABLE_IOS(3_0); // reloads the index bar.
刷新index这一部分,而不是刷新整个TableView;- (CGRect)rectForSection:(NSInteger)section; // includes header, footer and all rows
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
这几个方法是用来返回某一指定方块区域的。- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
得到某个点在TableView中的indexpath。- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; // returns nil if cell is not visible
返回指定Cell的IndexPath。- (NSArray *)indexPathsForRowsInRect:(CGRect)rect; // returns nil if rect not valid
某一块区域的Cell集合。- (NSArray *)visibleCells;
返回可见Cell集合
- (NSArray *)indexPathsForVisibleRows;
返回可见的Cell集合的indexPath集合。- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
返回某个指定Section的头和尾。TableView的方法:
- (void)beginUpdates; // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable
- (void)endUpdates; // only call insert/delete/reload calls or change the editing state inside an update block. otherwise things like row count, etc. may be invalid.
使用方法参考:(对TableView进行一系列操作时,使用这个来同步更新视图)http://blog.sina.com.cn/s/blog_7b9d64af0101b82p.html
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSectionNS_AVAILABLE_IOS(5_0);
移动Section
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPathNS_AVAILABLE_IOS(5_0);
移动row// Editing. When set, rows show insert/delete/reorder controls based on data source queries
@property(nonatomic,getter=isEditing)BOOL editing; // default is NO. setting is not animated.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
切换编辑状态。@property(nonatomic)BOOL allowsSelectionNS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode
设置为NO则所有Cell都无法被选中。@property(nonatomic)BOOL allowsSelectionDuringEditing; // default is NO. Controls whether rows can be selected when in editing mode
设置在编辑状态下能否选中@property(nonatomic)BOOL allowsMultipleSelectionNS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously
设置是否能够多选。@property(nonatomic)BOOL allowsMultipleSelectionDuringEditingNS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously in editing mode
编辑状态下能否多选。--------------
- (NSIndexPath *)indexPathForSelectedRow; // returns nil or index path representing section and row of selection.
- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0);// returns nil or a set of index paths representing the sections and rows of the selection.
返回当前选中的row的indexPath;// Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.
直接代码选中某一行或者取消选中某一行,但是不会触发上面的两个事件。
nsindexpath创建参考:http://blog.csdn.net/mamong/article/details/8496033
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
@property(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;
当我们tableView中section有很多,数据量比较大的时候我们可以引入indexList,来方便完成section的定位,例如系统的 通讯录程序。我们可以通过设置tableView的sectionIndexMinimumDisplayRowCount属性来指定当 tableView中多少行的时候开始显示IndexList,默认的设置是NSIntegerMax,即默认是不显示indexList的。
为了能够使用indexlist我们还需要实现dataSource中一下两个方法:
12- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
第一个方法返回用于显示在indexList中的内容的数组,通常为A,B,C...Z。第二个方法的主要作用是根据用户在indexList 中点击的位置,返回相应的section的index值。这个例子可以在苹果官方给出的TableViewSuite中找到,实现起来还是很简单的。
@property(nonatomic,retain) UIColor *sectionIndexColorNS_AVAILABLE_IOS(6_0)UI_APPEARANCE_SELECTOR; // color used for text of the section index
@property(nonatomic,retain) UIColor *sectionIndexTrackingBackgroundColorNS_AVAILABLE_IOS(6_0)UI_APPEARANCE_SELECTOR;// the background color of the section index while it is being touched
索引外观自定义。@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; // default is UITableViewCellSeparatorStyleSingleLine
@property(nonatomic,retain) UIColor *separatorColor; // default is the standard separator gray
定义分割线的类型和颜色;可以在这里将分割线设置为none。------------------------------------
@property(nonatomic,retain) UIView *tableHeaderView; // accessory view for above row content. default is nil. not to be confused with section header
设置整个UITableView最上面的一个视图,与sectionHeader没有任何关系。它也会像一个cell一样在向下滑的时刻被遮住。也就是在tableview最上面放置一个UIView,会将其他cell挤下去。
@property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer
同上。- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
返回已经alloc的可复用的cell通过指定的string标识符。
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
将指定indexPath的cell重用(若不能重用则返回nil,在StoryBoard会自动生成一个新的cell)。
{IOS6新特性:对复用机制的改进
// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
// Instances returned from the new dequeue method will also be properly sized when they are returned.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);
}
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0); // like dequeueReusableCellWithIdentifier:, but for headers/footers
复用相同标志符的Header和Footer view;
// Editing
// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
设置某行能够被编辑。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
设置某行能否被移动。- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1))
设置索引列表和索引对应关系。// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
编辑操作的回调方法,例如点击删除之后应该怎么做就应该在这个方法中进行。// Data manipulation - reorder / moving support
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
移动或者是重新排序调用的回调方法。最后记住:
UITableView是继承自UIScrollView的,所以,很多的UIScrollView的属性都是可以使用的。例如PageEnable。contentOffset等。