In my app,I was trying to implement swipe to delete function for TableView.I followed the following codes.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath: (NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//code to delete
}
}
UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
But the issue is that commitEditingStyle method gets called about 1/20 times only.What can be the issue??Thank you.PLease help.
Related
I am showing UIMenuController using UITableView delegates,
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0){
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
if(action == #selector(copy:)){
return YES;
}
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
if(action == #selector(copy:)){
NSLog(#"Action");
}
}
But Is there any way to get the touch location in the UITableViewCell?
The easiest way is: Create custom UITableViewCells which have clear (invisible) buttons over the "hot spots" you care about someone touching.
I am using UITableViewController, I want to swipe delete cell. Already connected to dataSource and delegate.
it randomly detecting,
some times console prints
attempting to set a swipe to delete cell when we already have one....that doesn't seem good
He is my code,
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
Edit
My cellForRow AtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFavouriteCell forIndexPath:indexPath];
[self customizeFavouriteCell:cell withIndexPath:indexPath];
return cell;
}
You need to delete the data for the cell, and delete the cell. Using deleteRowsAtIndexPaths:withRowAnimation:.
Inside this protocol function:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// ... delete data here ...
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
Let me know if you have a more specific question about doing either of these.
You can use this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
I have a uitableviewdelegate class that is delegate of two uitableview. I want that one of them has remove button and the other do not have remove button. I used below code:
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView==self.firstTableView){
return #"Remove";
}else
return #"";
}
button my code do not disable editing style for secondTableView it just set empty text as title. How can I do this?
EDIT
I also use below methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == slef.FirstTableView){
//Some codes
}
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.FirstTableView){
//Some codes
}
}
but any of them was not helpful.
Please implement this delegate method.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==self.firstTableView) {
return NO;
}
return YES;
}
Use delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
Here you can return UITableViewCellEditingStyleNone or UITableViewCellEditingStyleDelete for desired table
Occasionally, when UITableView is in editing style and when you trigger the delete button, this button may not show. It seldom happen, but did anybody get the same issue too?
Post some code:
In viewDidLoad,
[[self tableView] setEditing:YES animated:YES];
table view delegate:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//do sth.
}
}
Is there a simple way to implement the copy menu when a cell is tapped, instead of subclassing the UITableViewCell?
thanks,
RL
In iOS 5, a simple way is to implement the UITableViewDelegate Methods:
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
- (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
By implementing the 3 delegates, it will enable call UIMenuController for you after a long press gesture. An example like:
/**
allow UIMenuController to display menu
*/
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
/**
allow only action copy
*/
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
return action == #selector(copy:);
}
/**
if copy action selected, set as cell detail text
*/
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == #selector(copy:))
{
UITableViewCell* cell = [tableView cellForIndexPath:indexPath];
[[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
}
}
Yes!
Call [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (where ani is a BOOL determining whether the controller should be animated) from within - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ( UITableView's delegate method)
Edit: The 'copy' command on the UIMenuController will not by default copy the detailTextLabel.text text. However, there is a workaround. Add the following code into your class.
-(void)copy:(id)sender {
[[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == #selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}