Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
UITableViewDataSource: Difference between revisions - iPhone Development Wiki

UITableViewDataSource: Difference between revisions

From iPhone Development Wiki
(Created page with ''''UITableViewDataSource''' is a protocol that interfaces between a UITableView and its data model. == Text fields in cells == {{function signature |signature=-(void)tableVi…')
 
(added info on custom menu items on long press cell)
Line 13: Line 13:
|firmware=3.1 –
|firmware=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.
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)
<source lang="objc">
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, "v@:@");
}
return originalValue;
}
%end
</source>


== References ==
== References ==
* Official reference: {{sdklink|UIKit|UITableViewDataSource}}
* Official reference: {{sdklink|UIKit|UITableViewDataSource}}
{{navbox Classes|UIKit.framework}}
{{navbox Classes|UIKit.framework}}

Revision as of 06:00, 9 April 2014

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, "v@:@");
	}
	return originalValue;
}
%end

References