UITableViewCell prevent deletion - objective-c

I am looking for a way of preventing the deleting of one of my cells. (No delete button should appear next to the cell when the table view is in editing mode.)
How can this be made possible?

Implement editingStyleForRowAtIndexPath and return UITableViewCellEditingStyleNone for that row:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == sss && indexPath.row == rrr)
return UITableViewCellEditingStyleNone;
else
return UITableViewCellEditingStyleDelete;
}

The accepted response works but is not the correct way to do it. There are two methods available: editingStyleForRowAtIndexPath and canEditRowAtIndexPath
editingStyleForRowAtIndexPath: Use when there are multiple different editing styles in the table
canEditRowAtIndexPath: Use when some rows should edit and some should not.
Therefore the correct way to implement your table delegate is:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == sss && indexPath.row == rrr)
{
return NO;
}
return YES;
}

Related

UITableViewCell like mail on iphone

How can I adjust a UITableViewCell so that it would show two fields after swiping it left - identical to how the mail client does it?
Right now I only see the "Delete" button.
I would like to have the "More" field included there, too...
Also, for the first if clause Cell I do not get a "Insert" field.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.reports.count){
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
Is there a way to combine the fields with the | operator or something?
Thanks, EL-
You can use undocumented delegate methods for UITableView in iOS7
- (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"More";
}
- (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setEditing:NO animated:YES];
}
It seems that they are not private and can be submitted to appStore.

Making a conditional for table view buttonpress

I have a table view and don't know how to access the row number when the user presses one of the cells. The data of the table view is a small array and this is the closest I could come... In the IB, I control dragged the reusable cell to the view controller I push which works fine. I am unsure how to get the row number in a conditional like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// A bunch of stuff is before this, but not important to the question
//-- > This line if([indexPath row] == 1) This doesn't work... any ideas?
{
if(totalPoints < 10 && number != 0)
{
[self saveNumber:#"1"];
}
else if (totalPoints < 10 && attachment == 0)
{
[self saveData:#"1"];
int newPnts = totalPoints + 1;
[self savePoints:[NSString stringWithFormat: #"%d", newPnts]];
}
Thanks
It should work
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",indexPath.row);
if(indexPath.row==1)
{
NSLog(#"Do Somthing..");
}
else
{
NSLog(#"Do Somthing..");
}
}
the row number is [indexPath row] . in tableview you have a dataSource which save the data array used to reload table
Ok I was doing something wrong elsewhere and the view controller doesn't update it's label from the saved data. I will post a seperate question if you're interested

UITableView in editing mode cell still selectable even when using

I have a UITableView that has AllowsMultipleSelectionDuringEditing set to YES. Selecting multiple rows is a big part of my app, and it works great.
The problem is, that on one of my tableView's I don't want the user to be able to select the first 2 rows when in editing mode, so I have implemented the following:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 || indexPath.row ==1) {
return NO;
}//end
return YES;
}//end
This works how I thought it would, and doesn't show the red checkbox graphic or the option to re-sort the rows. BUT, I can still select those rows that are not editable, and call the indexPathsForSelectedRows method and return the indexPaths of those rows.
How can I prevent the user COMPLETELY from being able to select those rows while in editing mode, and prevent touches on those from being returned when calling indexPathsForSelectedRows? Why isn't canEditRowAtIndexPath: doing this for me?
One way to do this would be to implement – tableView:willSelectRowAtIndexPath: method and check if tableView.editing == YES then return nil for first two cells.
Something like,
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.editing == YES && indexPath.row < 2 )
return nil;
return indexPath;
}
You can also set the selectionStyle of these two cells as UITableViewCellSelectionStyleNone in editing mode in cellForRowAtIndexPath method.
Did you try the following code?
tableView.allowsSelectionDuringEditing = NO;
or
tableView.allowsMultipleSelectionDuringEditing = NO;

Changing in Code Specific Row Heights in Objective-C

I have a UITableView that uses prototype cells to recreate a table. I have the first row with height 30 and the rest default 44. I would like to change only first row's height but have failed to do so. I tried cellForRowAtIndexPath but I learned here that it's heightForRowAtIndexPath: I'm interested in. However, all the IndexPath's that get sent to it are [0,0]. Can anyone help?
Have you tried this?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { //first row
return 30;
}
else {
return 44;
}
}
Do this for resizing the cells in your table views delegate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return 30.0;
return 44.0;
}
If this doesn't work: give us your code

Question about UITableView

I have a UiTableView and the first cell is only showing information. The following cells are going to push new viewControllers to the window.
I don't want it to be possible to click on the first one, but the second cell (indexpath.row == 1) to be the first cell to click on...
Is there some property I can set to make that first cell "notChooseable"?
try:
if(indexPath.row == 0)
cell.userInteractionEnabled = NO;
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return;
}
else {
//perform pushing controllers
}
}