October 2013
Intermediate to advanced
1053 pages
28h 7m
English
The accessories provided to you by the iOS SDK are not sufficient, and you would like to create your own accessories.
Assign an instance of the UIView class to the accessoryView property of any instance
of the UITableViewCell class:
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*cell=nil;cell=[tableViewdequeueReusableCellWithIdentifier:MyCellIdentifierforIndexPath:indexPath];cell.textLabel.text=[NSStringstringWithFormat:@"Section %ld, Cell %ld",(long)indexPath.section,(long)indexPath.row];UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];button.frame=CGRectMake(0.0f,0.0f,150.0f,25.0f);[buttonsetTitle:@"Expand"forState:UIControlStateNormal];[buttonaddTarget:selfaction:@selector(performExpand:)forControlEvents:UIControlEventTouchUpInside];cell.accessoryView=button;returncell;}
As you can see, this code uses the performExpand: method as the selector for each
button. Here is the definition of this method:
-(void)performExpand:(UIButton*)paramSender{/* Handle the tap event of the button */}
This example code snippet assigns a custom button to the accessory view of every row in the targeted table. The result is shown in Figure 4-4.

Figure 4-4. Table view cells with custom accessory views
An object ...
Read now
Unlock full access