December 2012
Intermediate to advanced
834 pages
27h
English
You would like to respond to various events that a table view can generate.
Provide your table view with a delegate object.
Here is an excerpt of the .h file of a view controller with a table view:
#import <UIKit/UIKit.h>@interfaceReceiving_and_Handling_Table_View_EventsViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>@property(nonatomic,strong)UITableView*myTableView;@end
The .m file of the same view
controller implements a method defined in the UITableViewDelegate protocol:
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{if([tableViewisEqual:self.myTableView]){NSLog(@"%@",[NSStringstringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);}}-(void)viewDidLoad{[superviewDidLoad];self.myTableView=[[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;self.myTableView.dataSource=self;self.myTableView.delegate=self;[self.viewaddSubview:self.myTableView];}
While a data source is responsible for providing data to the table view, the table view consults the delegate whenever an event occurs, or if the table view needs further information before it can complete a task. For instance, the table view invokes a delegate’s method:
When and before a cell ...
Read now
Unlock full access