簡體   English   中英

UITableView 的委托和數據源方法

[英]delegate and datasource methods for UITableView

誰能列出UITableView的委托方法和數據源方法?

UITableView的代表和數據源方法是否相同?

我已經准備了委托和數據源中所有方法的表格。

UITableViewDelegate 方法

//_______________________________________________________________________________________________________________
// this represents the display and behaviour of the cells.

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

@optional

// Display customization

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

// Variable height support

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// Section header & footer information. Views are preferred over title should you decide to provide both

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

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0);
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

// Selection

// 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 __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_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;
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_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

@end
//_______________________________________________________________________________________________________________
    // this protocol represents the data model object. as such, it supplies no information about appearance (including the cells)

@protocol UITableViewDataSource<NSObject>

@required

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

@optional

- (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#")
- (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;

@end

列出他們?? 不,它們不一樣..查看文檔.. http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

數據源方法用於在顯示之前生成 tableView 單元格、header 和頁腳。委托方法提供有關這些單元格的信息,header 和頁腳以及其他用戶操作處理程序,如單元格選擇和編輯。

   tableView:accessoryButtonTappedForRowWithIndexPath:   
   tableView:didDeselectRowAtIndexPath:   
   tableView:didEndEditingRowAtIndexPath:   
   tableView:didSelectRowAtIndexPath:   
   tableView:editingStyleForRowAtIndexPath:   
   tableView:heightForFooterInSection:   
   tableView:heightForHeaderInSection:   
   tableView:heightForRowAtIndexPath:  
   tableView:indentationLevelForRowAtIndexPath:   
   tableView:shouldIndentWhileEditingRowAtIndexPath:   
   tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:   
   tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:   
   tableView:viewForFooterInSection:   
   tableView:viewForHeaderInSection:   
   tableView:willBeginEditingRowAtIndexPath:   
   tableView:willDeselectRowAtIndexPath:   
   tableView:willDisplayCell:forRowAtIndexPath:   
   tableView:willSelectRowAtIndexPath:  

看看

我不明白你問題的最后一部分。

Apple 提供了一個名為“iOS 開發中心”的好地方,它可以回答幾乎所有關於為 iOS 設備開發的問題。 此信息也可通過 Xcode 獲得。

UITableDataSource

UITableViewDelegate

數據源方法:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

委托方式:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}



- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{}

這是我們用來制作表格視圖的主要方法。 除此之外還有很多方法。 閱讀 UITableView 代表和數據源文檔以了解更多方法及其工作原理。

Go 鏈接,它將描述UITableView的所有基礎知識。

UITableView的主要方法是:

– tableView:cellForRowAtIndexPath: // 需要的方法

– tableView:numberOfRowsInSection: // 需要的方法

– numberOfSectionsInTableView:

– sectionIndexTitlesForTableView:

– tableView:sectionForSectionIndexTitle:atIndex:

– tableView:titleForHeaderInSection:

– tableView:titleForFooterInSection:

必需的是您必須包含在代碼中的那些。 不要忘記將表委托和數據源與文件所有者鏈接。

哪個方法返回一個值,即 dataSource 方法,其他剩余方法是委托方法

下面給出

委托方法

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

數據源

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//Delegate & DataSource 方法 Deferent.

//斯威夫特 4.1

UITableViewDataSource: // 下面兩個方法需要

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

//可選的

func numberOfSections(in tableView: UITableView) -> Int

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?

//行編輯

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool

// 指數

func sectionIndexTitles(for tableView: UITableView) -> [String]?

func tableView(_ tableView:UITableView,sectionForSectionIndexTitle 標題:字符串,索引處:Int)-> Int

// 行插入或刪除

func tableView(_ tableView: UITableView, 提交editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

func tableView(_ tableView:UITableView,moveRowAt sourceIndexPath:IndexPath,到destinationIndexPath:IndexPath)

//UITableViewDelegate //行,header,頁腳高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat

// 行選擇

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

讓我試着用簡單的方式給你解釋一下,我以前理解的方法。 代表只是跟蹤已完成的事情或在單元格上執行的操作。 DataSource 為您提供了定義表格外觀的功能。

話雖如此,代表的功能是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{}

數據源

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

#pragma mark Tableview Delegate Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"TableviewCell"];

    UILabel *lbldescription = (UILabel *)[cell viewWithTag:1];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor clearColor];
    return cell;
}

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

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"TableviewCell_Header"];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor whiteColor];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM