From iPhone Development Wiki
Jump to: navigation, search

UITableViewDataSource is a protocol that interfaces between a UITableView and its data model.

Text fields in cells

Signature -(void)tableView:(UITableView*)tableView didUpdateTextFieldForRowAtIndexPath:(NSIndexPath*)indexPath withValue:(NSString*)value;
Available in 3.0 –

If a UITableViewCell has an associated text field, the data source may implement this method to catch updates to it by the user.

Copy and paste

Signature -(void)tableView:(UITableView*)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender;
-(BOOL)tableView:(UITableView*)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender;
-(BOOL)tableView:(UITableView*)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath*)indexPath;
Available in 3.1 –

Starting from 3.1, table view cells may invoke the UIMenuController by long-pressing the cell without 3rd-party extensions. The data source must implement the above 3 methods to support it. Only the copy, paste and cut actions are supported. Custom menu items will not show in the menu even if you return true for their actions in the tableView:canPerformAction:forRowAtIndexPath:withSender: method.

You can use the following code to allow custom menu items, and then return true for their actions in the above method as with copy/paste/cut. (This works in iOS 7)

void tvcForwardAction(id self, SEL _cmd, id sender) {
	[self _performAction:_cmd sender:sender];

%hook UITableView
- (BOOL)_canPerformAction:(SEL)action forCell:(id)cell sender:(id)sender {
	BOOL originalValue = %orig;
	if (originalValue && ![UITableViewCell instancesRespondToSelector:action]) {
		class_addMethod([UITableViewCell class], action, (IMP)tvcForwardAction, "[email protected]:@");
	return originalValue;