2011-04-29 105 views
11

任何人都可以列出UITableView的委托方法和数据源方法吗?UITableView的委托和数据源方法

代表和数据源方法是否相同UITableView

+8

有没有听说过文档? – 2013-10-01 14:31:46

+0

你甚至不打算对文档进行粗略的搜索。 – Jon 2014-05-06 17:44:40

+3

为什么这个问题存在?谁会保持最新?最新的文档公开可在http://developer.apple.com – CrimsonChris 2014-08-27 16:44:13

回答

2

列出他们?不,他们是不一样.. 看看文件.. 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细胞,页眉和页脚他们之前displaying..Delegate方法提供有关这些单元的信息,头,并与其他用户操作处理程序像小区选择和编辑一起页脚..

2

我不明白你的问题的最后一部分。

苹果公司提供了一个名为“iOS开发中心”的好地方,它可以回答你关于开发iOS设备的几乎所有问题。这些信息也可以通过Xcode获得。

UITableDataSource

UITableViewDelegate

5
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: 

具有look

+0

哈哈..你确实列出了他们..我也想过第一次..做得好.. :) – Krishnabhadra 2011-04-29 12:21:30

+0

哈哈..投票答案如果你喜欢它.. – Saurabh 2011-04-29 12:51:17

38
//_______________________________________________________________________________________________________________ 
// 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 
+1

这应该奠定了一个主意:) https://gist.github.com/4479072 – 2013-01-07 22:22:41

+0

这个答案是过时的。请访问http://developer.apple.com获取最新的文档。 – CrimsonChris 2014-08-27 18:34:07

39

我已经准备的代表中和数据源的所有方法的片材。

UITableViewDelegate methods

1

转到link,它将描述UITableView所有的基础知识。的UITableView

主要方法是:

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

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

– numberOfSectionsInTableView: 

– sectionIndexTitlesForTableView: 

– tableView:sectionForSectionIndexTitle:atIndex: 

– tableView:titleForHeaderInSection: 

– tableView:titleForFooterInSection: 

需要一个是那些你必须在代码中包含的。 不要忘记将表代表和数据源与文件所有者链接起来。

1

而且,任何方法返回一个值,这是一个数据源方法和其他剩余的方法是委托方法

下面给出

委托方法

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

数据源方法:

-(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委托和数据源文档以了解更多方法及其工作。

0
#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; 
} 
相关问题