Hi,
Here we have a list of all the methods and their descriptions for UITableView(UITableViewDelegate and UITableViewDataSource) Protocols.
UITableViewDelegate Protocol:-
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
@optional
customization
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
TableView sends this message to its delegate just before it uses cell to draw a row and to customize the cell object before it displayed. This method is often used for overriding state based properties set earlier by the table view like selection and background color.
Height support
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method allows us to specify rows with varying heights. (Returned Height should be less than are equal to 2009) If we implement this method this overrides the rowHeight property of the tableview.
– (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
Description:-
This method allows us to specify section headers with varying heights (From iOS 5.0 and later we must return the actual height for each section header).
– (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
Description:-
This method allows us to specify section footers with varying heights (works only in table view style grouped).
Header and Footer Sections
– (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Description:-
This method allows us to customize the header view of a section it returns an object that can be UILabel or UIImageView or a customView this method only works correctly when we implement heightForHeaderInSection method also. And this will be adjusted to default or specified header height.
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
Description:-
This method allows us to customize the footer view of a section it returns an object that can be UILabel or UIImageView or a customView this method only works correctly when we implement heightForFooterInSection method also. And this will be adjusted to default or specified footer height.
Accessories (disclosures).
– (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
Description:- (Deprecated in iOS 3.0).
– (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
Description:-
This method responds when we tap on disclosure button. It won’t respond, If it is called when an accessory view is set for the row at indexPath.
// Selection
These methods are called before the user changes the selection and return a new indexPath, or nil, to change the proposed selection.
– (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method called when the user tap and release the touch even though the row is highlighted when the user touches it won’t get called it called when the user releases the touch on row. And it won’t be called when the table is in editing mode.
– (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method is only called if there is an existing selection when the user tries to select a different row. The delegate is sent this method for the previously selected row.
These methods are called after the user changes the selection.
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method we will use mostly and is called when we select a cell and it can be use to set disclosure images when we select a row.This method is not called when the editing property of the table is set to yes.
– (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
Row deselections handled in this method for example we can remove the disclosure images associated with the row.
For Editing
Those methods 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;
Description:-
This methods allows us to customize the editing style of a cell located at indexPath.
– (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
We can delete a row either by swapping or tapping the red minus icon in editing mode that time Delete button has a title like “Delete”.
we can implement this method to return an alternative title.
– (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method controls whether the background is indented while editing.
If this method is not implemented, the default value is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views.
– (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
– (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
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. In swipe to delete mode the table view does not display any insertion, deletion, and reordering controls. These methods give the delegate an opportunity to adjust the application’s user interface to editing mode.
Moving or Reordering
– (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
Description:-
This method allows customization of the target row for a particular row as it is being moved/reordered. As the dragged row hovers over another row, the destination row slides downward to visually make room for the relocation (This is the location identified by proposedDestinationIndexPath).
Indentation
– (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method returns depth of row for hierarchies.
@end
UITableViewDataSource Protocol:-
This protocol contains some required methods to implement when we implement this protocol.
This protocol represents the data model object and such as it supplies no information about appearance it only deals with the data.Data source provides the table-view object with the information it needs to construct and modify a table view.
@protocol UITableViewDataSource<NSObject>
@required
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
Description:-
This is the required method to implement and it returns the number of rows in a particular section.
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This is 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).It returns a cell which is reusable and this is the place where we can customize our cell data and this is the important method of all.
@optional
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
Description:-
This method reruns the number of sections in a tableview if you don’t implement this method the default number of sections are one.
– (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
– (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
Description:-
It returns string to use as the title of the section header if it is nil header will be displayed with the empty title. We can use custom view for different font styles.
Editing
– (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method allows us to individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
Moving or Reordering
– (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
This method 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:. If it returns YES that means row can be moved other wise row cannot be moved.
Index
– (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
Description:-
This method returns an array of section titles to display in section index view (e.g. “A” through “Z” ( “ABCD…Z#”)) and the table view must be in the plain style.
– (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
Description:-
This method tell table which section corresponds to section title/index (e.g. “B”,1).There are two index numbers those are an index to an section index title in the array returned by sectionIndexTitlesForTableView:, and an index to a section of the table view; the former is passed in, and the latter is returned.
Data manipulation – insert and delete support
– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
Description:-
After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change.
Data manipulation – reorder / moving support
– (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
This method is used for reordering of the rows.
@end
When I Write this Blog I took the help of below links.
Thank You.
Source:-
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:willDisplayCell:forRowAtIndexPath:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:moveRowAtIndexPath:toIndexPath:
http://stackoverflow.com/questions/5831813/delegate-and-datasource-methods-for-uitableview