UIScrollView && UITableView相關屬性彙總及常用方法

屬性:

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;



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章