UITableView, Catching touches on delete? - iphone

I wonder if someone would be so kind as to help me out with a query I have regarding deleting rows from a UITableView. I have implemented tableView:commitEditingStyle:forRowAtIndexPath: and now get the [DELETE] button appearing when I swipe on a cell, my question is which method do I need to use to catch a user pressing that button (i.e. [DELETE]).
Also when I do delete a row, can I delete the row from my dataSource and refresh the table. OR should I delete the row from the dataSource and then call some other method on the tableView?

This is what I was after, the callback for the swipe and the callback when the user selects [DELETE].
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Swipe ...");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Delete Pressed.");
}

Delete from the data source and call reloadData on the table. Or do you have like a million rows in the table?

Related

getting indexpath while reordering cells in tableview?

I want change the cells order in my tableview
I am using following code
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
[tableView moveRowAtIndexPath:path1 toIndexPath:path2];
}
here path1 indexpath of row which user selected now I want find the indexpath of the row the user drop that cell so how can I find that toIndexPath(path2).
I think you're confused with the purpose of this method - this is a callback function of the UITableViewDataSource that's designed to notify the data model of changes in the table. From the docs:
Tells the data source to move a row at a specific location in the table view to another location.
The UITableView object sends this message to the data source when the user presses the reorder control in fromRow.
This means that what you need to do here is to make sure your model (Usually an array) is rearranged to sync properly with these changes.
Hope this gives you a good direction.
You should implement:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
& also:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//do whatever you want after the row has been moved
}
Here is an example of drag N drop:
https://github.com/coderDove/UITableView-Drag-n-Drop
Hope this Helps!!!

when I click a UIButton I'm trying to find out which cells are currently selected inside a UITableView

I have 2 UITableViews. When I click the UIButton outside of either UITableView I'm trying to determine which cells are selected in each tableview.
Anyone have a good way of doing this? Would I loop over the cells to determine which cell is currently selected? Or is there already a method that can tell me?
Thanks a bunch!
You can use this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
Take a look at UITableViewDelegate Protocol Reference
UITableViewDelege Reference
EDIT: When the user select a row in one of your tables you can get it using this method and a couple of variables for the table and the cell and then you can pass what you got as soon as the user tap on your button.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
thisIsMyTable = tableView;
thisIsTheSelectedRow = indexPath.row;
}
- (void)buttonTapped {
// Do what you need
}

Objective C, How to load more data when user tries to scroll up at the bottom row of the tableview

I have UITableView and I want to load more data when user tries to scroll up at the bottom row of the tableview just like iPhone email application...What is the best way for this?
Implement the Delegate for UIScrollView and detect at which point where you want to load more data.
Or You can put the update code in the "cellForRowAtIndexPath:" method, here detect the row, say 100 and append the data, and remove the data in row <100 , you will have to check for dataSource's count before updating.
Append new contents to the Data Source (Array) and do this:
[self.tableView reloadData];
I tried adding this in UITableViewDelegate method "willDisplayCell"
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == floatRows - 1) {
floatRows += 10;
[tableView reloadData];
}
}
It is keep loading another 10 for every scroll to bottom.
Not sure, it will work for you.

Swipe to Delete not working

The swipe to delete functionality is not working in my table view. I have implemented the commitEditingStyle delegate and the Edit button in the navigation bar. Hence when the user clicks the edit button, the delete and add buttons show up appropriately. However, on swiping, the delete button does not appear and it seems like it doesn't recognise the swipe as a call for the setEditing method.
I then implemented willBeginEditingRowAtIndexPath and didEndEditingRwoAtIndexPath delegates as follows:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"WILL BEGIN EDITING");
[self.tableView setEditing:YES animated:YES];
}
-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView setEditing:NO animated:YES];
}
However this does not have any effect either. What could be the possible issue? I have enabled multi-touch for the table view in the IB and each cell has an DetailDisclosureButton accessory.
For swipe-to-delete functionality, you have to implement
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
; // Delete.
}
EDIT: I didn't read the question well enough.
I have noticed that to do a swipe drag in the simulator I have to press and pause for a second while it selects the cell, and only then can I swipe right successfully.
You'll need to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
With these three things you should be good to go. (I missed off the commitEditingStyle and it never worked.
If all other advices doesn't help try to check if you have pan gesture recognizer in your code. It will not work if you have things like this in some underlaying view.
I was doing the following in the tableView:editingStyleForRowAtIndexPath: delegate method :-
if (self.editing == NO || !indexPath)
{
return UITableViewCellEditingStyleNone;
}
This was supposed to return editing style none if no indexPath was selected. For some reason whenever a swipe action was performed, this particular if condition was getting executed. On commenting the above code snippet, the swipe to delete action worked fine.
Thanks all for your help.
The key to make it work is to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
And remember to put it inside "UITableViewDataSource" instead of "UITableViewDelegate"
did you implement the tableView:editingStyleForRowAtIndexPath: method and have it return UITableViewCellEditingStyleDelete
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath delegate method also need to implement to work swipe functionality .

How to disable the delete button for the particular row when we are using the table view delegate method commitEditingStyle in Iphone sdk?

Here I need a help from ur side that Im using the tableview delegate method commitEditing sytle to get the delete button for every cell when we swipe.But my problem here is I dont want the delete button for the first row in the tableview and swipe should not work and I dnt have any idea how to implemet this.
Anyone's help will be much appreciated.
Thank You,
Monish.
UITableViewCell has delete style by default. To change that you need to implement tableView:editingStyleForRowAtIndexPath: method in your table view delegate. Return UITableViewCellEditingStyleNone for the 1st row and UITableViewCellEditingStyleDelete for other rows.
Edit: Sorry for incomplete answer. You also need to implement tableView:canEditRowAtIndexPath: method (something like that):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.row > 0;
}
Hope that'll work
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(condition check)
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//here your code
}
}