UIScrollView && UITableView相关属性汇总及常用方法

来源:互联网 发布:java线程三种实现方式 编辑:程序博客网 时间:2024/05/18 00:57

属性:

UITableViewDatasource:实例化表视图时,必须采用该方法来实现数据源的配置
UITableViewDelegate:表视图的委托方法,一般用于处理表视图的基本样式以及捕捉选中单元格选中事件


1. UIScrollView:

tracking //当 touch 后还没有拖动的时候值是YES,否则NO

zoomBouncing //当内容放大到最大或者最小的时候值是 YES,否则 NO

zooming //当正在缩放的时候值是 YES,否则 NO

decelerating //当滚动后,手指放开但是还在继续滚动中。这个时候是 YES,其它时候是 NO

decelerationRate //设置手指放开后的减速率

maximumZoomScale //一个浮点数,表示能放最大的倍数

minimumZoomScale //一个浮点数,表示能缩最小的倍数

pagingEnabled //当值是 YES 会自动滚动到 subview 的边界。默认是NO

scrollEnabled //决定是否可以滚动

sView.contentSize = CGSizeMake(320*5,372);//滚动的范围


delaysContentTouches //是个布尔值,当值是 YES 的时候,用户触碰开始,scroll view要延迟一会,看看是否用户有意图滚动。假如滚动了,那么捕捉 touch-down 事件,否则就不捕捉。假如值是NO,当用户触碰, scroll view 会立即触发 touchesShouldBegin:withEvent:inContentView:,默认是 YES


canCancelContentTouches //当值是 YES 的时候,用户触碰后,然后在一定时间内没有移动,scrollView 发送 tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,scrollView 发送了 touchesCancelled:withEvent: 到 subview,然后 scroView 开始滚动。假如值是 NO,scrollView 发送 tracking events 后,就算用户移动手指,scrollView 也不会滚动。


contentSize //里面内容的大小,也就是可以滚动的大小,默认是0,没有滚动效果。

showsHorizontalScrollIndicator //滚动时是否显示水平滚动条

showsVerticalScrollIndicator //滚动时是否显示垂直滚动条

bounces //默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止。

bouncesZoom //和 bounces 类似,区别在于:这个效果反映在缩放上面,假如缩放超过最大缩放,那么会反弹效果;假如是 NO,则到达最大或者最小的时候立即停止。

directionalLockEnabled //默认是 NO,可以在垂直和水平方向同时运动。当值是 YES 时,假如一开始是垂直或者是水平运动,那么接下来会锁定另外一个方向的滚动。 假如一开始是对角方向滚动,则不会禁止某个方向

indicatorStyle //滚动条的样式,基本只是设置颜色。总共3个颜色:默认、黑、白

scrollIndicatorInsets //设置滚动条的位置


2,UITableView


tableView.bounces=NO//禁止拖动

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉边框

[cellView setSelectionStyle:UITableViewCellSelectionStyleNone];//禁止触发点击某行//实现某行的选中效果(点击某行变颜色,松开还是刚变的颜色,点击其他行这行颜色消失):[cellView setBackgroundColor:[UIColor clearColor]]; 


cellView.selectedBackgroundView = [[[UIView alloc] initWithFrame:cellView.frame] autorelease]; 

UIImageView *ia1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)];

 ia1.image=[UIImage imageNamed:@"ts_bg01.png"];

cellView.backgroundView=ia1;

UIImageView *ia2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)]; 

ia2.image=[UIImage imageNamed:@"ts_bg02.png"]; 

cellView.selectedBackgroundView=ia2;


方法:


//tableView的样式设置


typedef NS_ENUM(NSInteger, UITableViewStyle) {

    UITableViewStylePlain,              //无分区样式  

    UITableViewStyleGrouped             //多分区样式

};


//tableView滚动时,要滚动到的位置


typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {

    UITableViewScrollPositionNone,

    UITableViewScrollPositionTop,    

    UITableViewScrollPositionMiddle,   

    UITableViewScrollPositionBottom

};


//tableView的cell操作时的动画效果


typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

    UITableViewRowAnimationFade,    //淡入或淡出

    UITableViewRowAnimationRight,           //从右边滑入或滑出

    UITableViewRowAnimationLeft,    //从左边滑入或滑出

    UITableViewRowAnimationTop,    //从顶部滑入或滑出

    UITableViewRowAnimationBottom,    //从底部滑入或滑出

    UITableViewRowAnimationNone,            // 无动画效果

    UITableViewRowAnimationMiddle,          //保持cell的中心在中部

    UITableViewRowAnimationAutomatic = 100  //自动适配动画

};


//sectionIndexTitlesForTableView: 返回的字符数组中包含了此字符串 具有放大图标展示的效果

 此字符串只能用于第一标题


UIKIT_EXTERN NSString *const UITableViewIndexSearch


//如果从tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: 返回的标题不为空   那么从 tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: 返回的值将对有标题的cell或section的高度有制约


UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension



//自定义展示cell的内容


//cell即将展示时调用

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;


//表头即将展示时调用

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


//表尾即将展示时调用

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);



- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);


- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);



// 各种高度的适配


//设置每行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;


//设置表头的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;


//设置表尾的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;


// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.

// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);


// 表头、表尾信息 当你展示视图和表头时,视图优先于标题


//自定义头视图 使用默认高度或者一个具体的高度

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height


//自定义尾视图 使用默认高度或者一个具体的高度

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height


// Accessories (disclosures). 副件


//设置disclosure属性和样式

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 


//点击disclosure时的调用

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;


// Selection 选中


// -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 

// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.


//点击时是否高亮状态


- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);


//点击显示高亮状态之后

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);


//不高亮状态

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_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;


//将要不被选中

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);


// Called after the user changes the selection.


//已经选中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


//已经未选中

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);


// Editing  编辑


// 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;


//此时删除按钮为Delete,如果想显示为“删除” 中文的话,则需要实现此方法


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_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;


// 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;


// 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


// Copy/Paste.  All three methods must be implemented by the delegate.


//是否允许长按菜单(弹出副菜单选项)

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);


//是否允许每一个Action 

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);


//对一个给定的行告诉代表执行复制或粘贴操作内容,

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);


eg:

if (action==@selector(copy:)) {//如果操作为复制 
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];//黏贴板 
        [pasteBoard setString:cell.textLabel.text]; 




UIKIT_EXTERN NSString *const UITableViewSelectionDidChangeNotification;




- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;                // must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain



// Data 数据


- (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)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0);   // reloads the index bar.


// Info


- (NSInteger)numberOfSections;


- (NSInteger)numberOfRowsInSection:(NSInteger)section;


- (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; // returns nil if point is outside of any row in the table


- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;                      // returns nil if cell is not visible


- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;                              // returns nil if rect not valid 


- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;            // returns nil if cell is not visible or index path is out of range


- (NSArray *)visibleCells;


//检查桌面可见indexPaths

- (NSArray *)indexPathsForVisibleRows;


- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;


- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;


// Row insertion/deletion/reloading.


- (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.


- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;


- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;


- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);


- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);


- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);


- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);


// 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;



// Selection


- (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.


// Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.


- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;


- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


// Appearance


- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.


- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered


- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);  // like dequeueReusableCellWithIdentifier:, but for headers/footers


// 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 *)identifier NS_AVAILABLE_IOS(5_0);


- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


// 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;


// Moving/reordering


// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;


// Index


//侧面索引栏内容

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;              

// return list of section titles to display in section index view (e.g. "ABCD...Z#")


//侧面索引栏对应到tableView中得内容

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))


// Data manipulation - insert and delete support


// 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;



// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row


+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;



0 0
原创粉丝点击