Unable to swipe delete first row of table in iPhone app - iphone

I have an iPhone app that utilizes TableView to list tagged items that the user has saved. I have Swipe to Delete enabled for these items yet I'm running into an issue with the very first item in the table. All other items show the "Delete" button when swiped, but it does not work for the very first row.
I've searched and searched for an answer to this. I'd appreciate the help!
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == 0) {
return UITableViewCellEditingStyleNone;
}
return UITableViewCellEditingStyleDelete;
}

You should return UITableViewCellEditingStyleDelete from tableView:editingStyleForRowAtIndexPath: for all rows that should support delete.
UPDATE
I have explained what your code does in some added comments, so you can see the problem:
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == 0) {
// any row that returns UITableViewCellEditingStyleNone will NOT support delete (in your case, the first row is returning this)
return UITableViewCellEditingStyleNone;
}
// any row that returns UITableViewCellEditingStyleDelete will support delete (in your case, all but the first row is returning this)
return UITableViewCellEditingStyleDelete;
}

Related

How to prevent a UITableViewCell being moved away from its own section?

My UITableView has 3 sections. Only the cells in the 2nd sections are movable:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL canMove = NO;
if (indexPath.section == 1) {
canMove = YES;
}
return canMove;
}
However, a cell (B) in the 2nd section (originally contains A``B``C cells) can be moved to the 1st section (originally contains only the T cell):
 
How can I make sure cells are moved within its own section?
I think this will solve your problem
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if( sourceIndexPath.section != proposedDestinationIndexPath.section )
return sourceIndexPath;
else
return proposedDestinationIndexPath;
}
From Apple documentation
This method allows customization of the target row for a particular
row as it is being moved up and down a table view. As the dragged row
hovers over a another row, the destination row slides downward to
visually make room for the relocation; this is the location identified
by proposedDestinationIndexPath.

Add "Swipe to delete" but not in all cells

I want to add in my tableview the possibility to "swipe to delete", but I don't want this for the last cell of the table!
I can check in - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath if the indexPath.row is the last one, but what I want is that if the user swipes on the last cell, nothing will appear (while in the others cells appears the text "delete").
I've tried this
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != ([array count]-1)) {
return #"delete";
}
else {
NSString *a;
return a;
}
}
but of course it doesn't works (the app crashes).
I've tried with
return #"";
but the red button appears (with no text)!
What do you suggest me?
Thanks!
Try
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == [tableView numberOfRowsInSection:[indexPath section]] - 1)
{
return UITableViewCellEditingStyleNone;
}
return UITableViewCellEditingStyleDelete;
}
The app crashes since you return an uninitialized pointer. But even then, you're doing it wrong ;-)
You want to implement tableView:editingStyleForRowAtIndexPath: and return UITableViewCellEditingStyleDelete for all cells, except for the last. You need to return UITableViewCellEditingStyleNone for the last cell to prevent that it can be deleted.

Deleting Rows from a UITableView That has Multiple Sections

I'm developing an iPad Application, in which one of the screens has an embedded tableview with multiple sections. Each section is populated by its own array (array1, and array2).
I've created a button that puts this table into editting mode. However, I need to change my
$tableView:commitEditingStyle:forRowAtIndexPath
somehow to determine what section the selected row is in, and delete the entry from the associated array as well. Does anyone have any ideas?
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle == UITableViewCellEditingStyleDelete){
//This is the line i need to change...
[array1 removeObjectAtIndex:indexPath.row];
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Looks like you need to remove the row from the correct array... using a switch statement on the sections might work (but only if there's a set number of sections, which you seem to indicate there is)
So where you're removing the object from the array, make sure you're removing it from the correct array based on the section.
switch (indexPath.section) {
case 0
[array0 removeObjectAtIndex:indexPath.row];
break;
case 1
[array1 removeObjectAtIndex:indexPath.row];
break;
}
etc.
Do like this... Get the required section using
NSInteger section = [indexPath section];
if (section == 0)
{
// write your code for deleting rows in your 1st section
}
if (section == 1)
{
// write your code for deleting rows in your 2nd section
}
// This is the idea...
The indexPath variable has the properties row and section. So, you can do:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle == UITableViewCellEditingStyleDelete){
if (indexPath.section == 1) {
// do something
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is the method you need to determine which cell you have been selected.
If you just want to determine the actual section which is shown you can do that with indexPath.section
NSInteger section = [indexPath section];
Try this:
[[self.mainArrayList objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
The indexPath parameter of the commitEditingStyle:... method has the section and row of the cell. Perhaps I'm misunderstanding the question, can you clarify what you mean by 'delete it from the associated array' - do you have one array for each section in your table?

How to limit the delete option for first row in UITableView

HI
In my appliacation I want deletion control button will not work for first row except for all other rows.
Please can anybody tell me how it would be possible.
Thanks
Implement editingStyleForRowAtIndexPath method in table delegate and return style value depending on cell's index:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//Disable editing for 1st row in section
return (indexPath.row == 0) ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
}

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
}
}