How to limit the delete option for first row in UITableView - iphone

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

Related

UITableViewCell editing style for specific rows

I've been trying to delete particular rows in UITableView, using below delegate method
tableView commitEditingStyle:editingStyle forRowAtIndexPath:
My intension is to not to show the "Delete" button for particular cells only.
please any one can help me how to solve this.
Use following Datasource method of UITableView
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the apply specified item to be editable.
if("Your Condition") // such like indexPath.row == 1,... or whatever.
return YES;
else
return NO;
}
In short return YES on specific row/cell which you want to edit otherwise return NO;

Exclude UITableViewCell from editing

I have an edit button that when pressed puts the entire table into edit mode: tableView.editing=YES;
I want every cell to be in edit mode except for the last cell, which I want to remain in non-edit mode, always. Is there any way to do this?
Check it out!
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// this logic might work for you, or it mightn't if you have more than one section
return [tableView numberOfRowsInSection:indexPath.section] != (indexPath.row + 1)];
}

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

How to prevent UITableView from reserving space at the left of cell when editing is turned on?

I'm set editing mode for UITableView to have a possibility of cell reordering. UITableViewCellEditingStyleNone is returned by editingStyleForRowAtIndexPath: method for every cell, but it reserve some area on the left of cell. Is it possible to prevent such an area reserving, because I'm not need an insert or delete icon on left? In short, i want have a cell that occupate all available area and still can be reordered.
// UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
See the docs: You can set a boolean on the cell to make it not indent. Just add
cell.shouldIndentWhileEditing = NO;
to wherever you create your cell.
Set this to like 2 or 3
tableView:indentationLevelForRowAtIndexPath:
The shouldIndentWhileEditing property only works with grouped tables. I found that setting an indentation level of -3 does the job for plain tables. Is there a better way? Here's what I'm using now:
if (self.tableView.style == UITableViewStylePlain) {
cell.indentationLevel = -3;
} else if (self.tableView.style == UITableViewStyleGrouped) {
cell.shouldIndentWhileEditing = FALSE;
}
Do both
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
and
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
in your UITableViewDelegate. Otherwise the cell content is indented.

Unable to swipe delete first row of table in iPhone app

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