
BAMEasyTable
BAMEasyTable is a class which handles the mechanics of a UITableView allowing you to write significantly less code to implement a table. If you’ve just arrived here and don’t know what BAMEasyTable is please check out my BAMEasyTable or BAMEasyTable Demo posts first.
Using the Delegate
To use the delegate, you need to specify your class as the delegate for BAMEasyTable:
1 2 3 4 5 6 7 8 9
| - (IBAction )buttonPressed :(id)sender {
NSArray *yourArray = [[NSArray alloc ] initWithObjects :@"Item One", @"Item Two", nil];
BAMEasyTable *bamEasyTable = [[BAMEasyTable alloc ] initWithStyle :UITableViewStylePlain ];
bamEasyTable.delegate = self; [bamEasyTable loadTableFromArray :yourArray ];
[self.navigationController pushViewController :bamEasyTable animated :YES];
[bamEasyTable release ];
[yourArray release ];
} |
Next, your class needs to conform to the delegate. So you’ll need to place in your header file, like so:
1 2 3 4 5
| @interface MySuperCoolClass : UIViewController <BAMEasyTableDelegate> {
}
@end |
BAMEasyTable Delegate Methods
Selection Methods
bamEasyTable:didSelectObject:
bamEasyTable:didDeselectObject:
Cell Customization Methods
bamEasyTable:cellForCustomization:withObject:
bamEasyTable:cellForObject:
bamEasyTable:heightForCellWithObject:
bamEasyTable:titleForDeleteConfirmationButtonForRowWithObject:
Button Response Methods
bamEasyTableAddButtonPressed:
bamEasyTable:accessoryButtonTappedForRowWithObject:
Table Row Editing Methods
bamEasyTable:didRemoveItemAtIndexPath:
bamEasyTable:movedRowFromIndexPath:toIndexPath:
bamEasyTable:canEditRowAtIndexPath:
bamEasyTable:canMoveRowAtIndexPath:
Section Editing Methods
bamEasyTable:canRemoveSection:
bamEasyTable:didRemoveSection:
Header and Footer Methods
bamEasyTable:heightForHeaderInSection:
bamEasyTable:viewForHeaderInSection:withTitle:
bamEasyTable:heightForFooterInSection:
bamEasyTable:viewForFooterInSection:withTitle:
bamEasyTable:didSelectObject:
Tells the delegate that the specified row is now selected.
- (void)bamEasyTable:(BAMEasyTable *)easyTable didSelectObject:(id)selectedObject
easyTable
A BAMEasyTable object informing the delegate about the new row selection.
selectedObject
The object associated with the selected row.
Discussion
This method allows the delegate to handle the selection of a cell. For example, pushing a detail view for the selected item onto the navigation controller. The object associated with the cell is provided to allow the delegate to determine which cell was selected and to pull any context from the object. The index path is specifically not provided as the table may be displaying a search result which may have an index path different from what the delegate might expect.
bamEasyTable:didDeselectObject:
Tells the delegate that the specified row is now selected.
- (void)bamEasyTable:(BAMEasyTable *)easyTable didDeselectObject:(id)deselectedObject
easyTable
A BAMEasyTable object informing the delegate about the new row deselection.
selectedObject
The object associated with the selected row.
Discussion
This method allows the delegate to handle the deselection of a cell. For example, if custom cells are being provided by the delegate, a check mark could be deselected. The object associated with the cell is provided to allow the delegate to determine which cell was selected and to pull any context from the object. The index path is specifically not provided as the table may be displaying a search result which may have an index path different from what the delegate might expect.
bamEasyTable:cellForCustomization:withObject:
Provides the delegate with an opportunity to customize a cell before it is added to the table.
- (void)bamEasyTable:(BAMEasyTable *)easyTable cellForCustomization:(UITableViewCell *)cell withObject:(id)currentObject
easyTable
A BAMEasyTable object providing the delegate with a cell for customization.
cell
The table cell view that will be added to the table.
selectedObject
The object associated with the selected row.
Discussion
Allows the delegate to customize the cell that will be used in the table. For example, the delegate may wish to set the accessory type or the selection style for the cell. It is incredibly important that this delegate be lightweight to ensure speedy rendering of the table as it scrolls. The object associated with the cell is provided to allow the delegate to pull any necessary context from the object. The index path is specifically not provided as the table may be displaying a search result which may have an index path different from what the delegate might expect.
bamEasyTable:cellForObject:
Asks the delegate for a cell to insert into the table view.
- (UITableViewCell *)bamEasyTable:(BAMEasyTable *)easyTable cellForObject:(id)currentObject
easyTable
A BAMEasyTable object requesting the table view cell.
selectedObject
The object associated with the selected row.
Return Value
An object inheriting from UITableViewCell that the table view can use for the specified row. If nil is returned, the default cell for the row will be generated.
Discussion
This works the same as the UITableViewDataSource tableView:cellForRowAtIndexPath: delegate method with the exception that a nil value is valid as a return value. When a nil value is returned, the instance of BAMEasyTable will create a default cell for the row. This allows the delegate to only customize some cells while using the default for others.
The returned UITableViewCell object is frequently one that the application reuses for performance reasons. You should fetch a previously created cell object that is marked for reuse by sending a dequeueReusableCellWithIdentifier: message to tableView. The identifier for a reusable cell object is assigned when the delegate initializes the cell object by calling the initWithStyle:reuseIdentifier: method of UITableViewCell.
The object associated with the cell is provided to allow the delegate to pull any necessary context from the object. The index path is specifically not provided as the table may be displaying a search result which may have an index path different from what the delegate might expect.
bamEasyTable:heightForCellWithObject:
Asks the delegate for the height to use for a row with a specified object.
- (CGFloat)bamEasyTable:(BAMEasyTable *)easyTable heightForCellWithObject:(id)selectedObject
easyTable
A BAMEasyTable object requesting the row height.
selectedObject
The object associated with the selected row.
Return Value
A floating-point value that specifies the height (in points) that row should be.
Discussion
The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.
There are performance implications to using tableView:heightForRowAtIndexPath: instead of the rowHeight property. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
Important: Due to an underlying implementation detail, you should not return values greater than 2009.
bamEasyTable:titleForDeleteConfirmationButtonForRowWithObject:
Changes the default title of the delete-confirmation button.
- (NSString *)bamEasyTable
:(BAMEasyTable
*)easyTable titleForDeleteConfirmationButtonForRowWithObject
:(id)selectedObject
easyTable
A BAMEasyTable object requesting the title.
selectedObject
The object associated with the selected row.
Return Value
A localized string to used as the title of the delete-confirmation button.
Discussion
By default, the delete-confirmation button, which appears on the right side of the cell, has the title of “Delete”. The table view displays this button when the user attempts to delete a row, either by swiping the row or tapping the red minus icon in editing mode. You can implement this method to return an alternative title, which should be localized.
bamEasyTableAddButtonPressed:
Tells the delegate that the user tapped add button.
- (void)bamEasyTableAddButtonPressed:(BAMEasyTable *)easyTable
easyTable
A BAMEasyTable object informing the delegate about the tapped add button.
Discussion
The delegate usually responds to the tap on the add button by displaying a new view for entering a record to later be inserted into the table.
bamEasyTable:accessoryButtonTappedForRowWithObject:
Tells the delegate that the user tapped the accessory (disclosure) view associated within a row associated with the selected object.
- (void)bamEasyTable:(BAMEasyTable *)easyTable accessoryButtonTappedForRowWithObject:(id)selectedObject
easyTable
A BAMEasyTable object informing the delegate about the tapped accessory button.
selectedObject
The object associated with the selected row.
Discussion
The delegate usually responds to the tap on the disclosure button (the accessory view) by displaying a new view related to the selected row. This method is not called when an accessory view is set for the row.
bamEasyTable:didRemoveItemAtIndexPath:
Tells the delegate that a row had been removed from the table.
- (void)bamEasyTable
:(BAMEasyTable
*)easyTable didRemoveItemAtIndexPath
:(NSIndexPath *)indexPath
easyTable
A BAMEasyTable object informing the delegate about the new row removal.
indexPath
An index path locating the row in tableView.
Discussion
Once a user has selected “delete” from a row in the table, it will be removed from the table. the delegate is notified of the removal so that it can update its data accordingly. This will not be called when the table is displaying search results.
bamEasyTable:movedRowFromIndexPath:toIndexPath:
Tells the delegate a row at a specific location in the table view was moved to another location.
- (void)bamEasyTable
:(BAMEasyTable
*)easyTable movedRowFromIndexPath
:(NSIndexPath *)fromIndexPath toIndexPath
:(NSIndexPath *)toIndexPath
easyTable
A BAMEasyTable object informing the delegate about the new row relocation.
fromIndexPath
An index path locating the row to be moved in tableView.
toIndexPath
An index path locating the row in tableView that is the destination of the move.
Discussion
The BAMEasyTable object sends this message to the delegate when the user uses the reorder control in a row to move it to a new location. This will not be called when the table is displaying search results.
bamEasyTable:canEditRowAtIndexPath:
Asks the data source to verify that the given row is editable.
- (BOOL)bamEasyTable
:(BAMEasyTable
*)easyTable canEditRowAtIndexPath
:(NSIndexPath *)indexPath
easyTable
A BAMEasyTable object asking if the row is editable.
indexPath
An index path locating the row in tableView.
Return Value
YES if the row indicated by indexPath is editable; otherwise, NO.
Discussion
The method permits the delegate to exclude individual rows from being treated as editable. Editable rows display the insertion or deletion control in their cells. If this method is not implemented, the value set for BAMEasyTable’s allowRemoving property will determine whether the row is editable. Rows that are not editable ignore the editingStyle property of a UITableViewCell object and do no indentation for the deletion or insertion control. Rows that are editable, but that do not want to have an insertion or remove control shown. This will not be called when the table is displaying search results.
bamEasyTable:canMoveRowAtIndexPath:
Asks the delegate whether a given row can be moved to another location in the table view.
- (BOOL)bamEasyTable
:(BAMEasyTable
*)easyTable canMoveRowAtIndexPath
:(NSIndexPath *)indexPath
easyTable
A BAMEasyTable object asking if the row can be moved.
indexPath
An index path locating the row in tableView.
Return Value
YES if the row indicated by indexPath can be moved; otherwise, NO.
Discussion
This method allows the delegate to specify that the reordering control for a the specified row not be shown. By default, the reordering control is shown if BAMEasyTable’s allowMoving property is set to YES. This will not be called when the table is displaying search results.
bamEasyTable:canRemoveSection:
Asks the delegate whether a given section can be removed.
- (BOOL)bamEasyTable:(BAMEasyTable *)easyTable canRemoveSection:(NSUInteger)section
easyTable
A BAMEasyTable object asking if the section can be removed.
section
An index number identifying a section in tableView.
Return Value
YES if the section indicated by section can be removed; otherwise, NO.
Discussion
The method permits the delegate to exclude individual sections from being removed. If this method is not implemented, the section will be removed when it becomes empty. This will not be called when the table is displaying search results.
bamEasyTable:didRemoveSection:
Tells the delegate that the specified section has been removed.
- (void)bamEasyTable:(BAMEasyTable *)easyTable didRemoveSection:(NSUInteger)section
easyTable
A BAMEasyTable object informing the delegate that a section has been removed.
section
An index number identifying a section in tableView.
Discussion
Once a user has deleted a row from the table, if the section becomes empty BAMEasyTable will automatically remove the section from the table unless the delegate implements bamEasyTable:canRemoveSection: and returns NO. The delegate is being notified of the removal so that it can update its data accordingly. This will not be called when the table is displaying search results.
bamEasyTable:heightForHeaderInSection:
Asks the delegate for the height to use for the header of a specified section.
- (CGFloat)bamEasyTable:(BAMEasyTable *)easyTable heightForHeaderInSection:(NSInteger)section
easyTable
A BAMEasyTable object requesting the header height.
section
An index number identifying a section in tableView.
Return Value
A floating-point value that specifies the height (in points) that header should be.
Discussion
This method allows the delegate to specify section headers with varying heights.
bamEasyTable:viewForHeaderInSection:withTitle:
Asks the delegate for a view object to display in the header of the specified section of the table view.
- (UIView
*)bamEasyTable
:(BAMEasyTable
*)easyTable viewForHeaderInSection
:(NSInteger
)section withTitle
:(NSString *)title
easyTable
A BAMEasyTable object requesting the header view.
section
An index number identifying a section in tableView.
title
A string value containing the current title for the header.
Return Value
A view object to be displayed in the header of section.
Discussion
The returned object, for example, can be a UILabel or UIImageView object. The table view automatically adjusts the height of the section header to accommodate the returned view object. This method only works correctly when bamEasyTable:heightForHeaderInSection: is also implemented.
bamEasyTable:heightForFooterInSection:
Asks the delegate for the height to use for the footer of a specified section.
- (CGFloat)bamEasyTable:(BAMEasyTable *)easyTable heightForFooterInSection:(NSInteger)section
easyTable
A BAMEasyTable object requesting the footer height.
section
An index number identifying a section in tableView.
Return Value
A floating-point value that specifies the height (in points) that header should be.
Discussion
This method allows the delegate to specify section headers with varying heights.
bamEasyTable:viewForFooterInSection:withTitle:
Asks the delegate for a view object to display in the footer of the specified section of the table view.
- (UIView
*)bamEasyTable
:(BAMEasyTable
*)easyTable viewForFooterInSection
:(NSInteger
)section withTitle
:(NSString *)title
easyTable
A BAMEasyTable object requesting the header view.
section
An index number identifying a section in tableView.
title
A string value containing the current title for the footer.
Return Value
A view object to be displayed in the footer of section.
Discussion
The returned object, for example, can be a UILabel or UIImageView object. The table view automatically adjusts the height of the section footer to accommodate the returned view object. This method only works correctly when bamEasyTable:heightForFooterInSection: is also implemented.