Renaming the "Delete" button in edit mode of tableviewcontroller - iphone

Can we rename the "Delete" button when put a table view in edit mode to something else?

The UITableView delegate just has to implement:
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

Implement
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
And return your custom title for delete.
From UITableViewDelegate docs

In Swift:
override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "Remove"
}

Code:
*- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"Rename"; // or put something else
}*

Related

TableView do not enter in willDisplayCell

My goal is define my own cell style, with background, font and size. I try this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ScreenListElements *currentScreenElement = [self.screenDefBuild.elementsToTableView objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentScreenElement.objectName];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:currentScreenElement.objectName];
}
cell.textLabel.text = currentScreenElement.objectName;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.backgroundColor = [UIColor blackColor];
}
I would like to change cell backgrond, but my app doesn't enter to willDisplayCell method. I have declared my class as:
#interface MyTableView : UIViewController <UITableViewDataSource,NSCopying> {
}
Should i have something else? Or maybe is a better way to declare own cell style.
#interface MyTableView : UIViewController <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}
the method
tableView:willDisplayCell:forRowAtIndexPath
is delegate method of UITableView
EDITED (to make it correct & accepted answer)
setting delegate
[myTableView setDelegate:self];
Ok, i saw, that you didn't implement the "UITableViewDelegate" protocol, only dataSource...
So it surely doesn't enter delegate methods.
Be sure it is like so:
#interface MyTableView : UIViewController <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}
Check UITableviewCell Delegate Protocol :
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor blackColor];
}
I had the same issue where I set all the things correct but if you have your function written manually you might miss the syntax and this error occurs:
(Instance method 'tableView(tableView:willDisplayCell:forRowAtIndexPath:) nearly
matches optional requirement tableView(_:willDisplay:forRowAt:) of
protocol UITableViewDelegate
This way the function is not going to be recognised, write the function correctly:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
If the delegate is assigned to self this will work, else it's gonna show the error as you write the code.

issue with swiping a UITableViewCell

I would like to do some stuff when a user swipes to the right of a UITableViewCell, so I use
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
However I tried to slide to the right and this didn't get invoked, why is this? All of my UITableView delegates are invoked.
I also have this in my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
All I want to do is to add a subview when a swipe happens
Do you have a tableView:commitEditingStyle:forRowAtIndexPath: method?
To quote Apple:
Note: A swipe motion across a cell does not cause the display of a Delete button
unless the table view's data source implements the
tableView:commitEditingStyle:forRowAtIndexPath: method.
And, deleting that method in my project also causes the willBeginEditing not to be called.
// You missed this?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)theTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"Remove";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope that helps.
Try adding this method to your tableView delegate methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[self.listTableView beginUpdates];
...
I assume because your other delegate methods are being called that you included in your .h file #interface line? If using IB did you right click on the tableview and wire up delegate and dataSource?
Set the property of table view
tableView.editing = YES;

UITableView does not go into "swipe to delete" mode

My UITableView is linked with my UIViewController instance. The UIViewController instance is the table's UITableViewDelegate and UITableViewDataSource.
Whenever a cell is swiped in the simulator, it does not trigger the "swipe to delete" mode.
I tried to provide implementations for the methods to support "swipe to delete" and table cell editing. None of them get called (none of the NSLogs are logging).
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView editingStyleForRowAtIndexPath");
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView canEditRowAt...");
return YES;
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView willBeginEditingRowAtIndexPath: %#", indexPath);
}
- (void)tableView:(UITableView *)tableView willEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView willEndEditingRowAtIndexPath: %#", indexPath);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle {
NSLog(#"tableView commitEditingStyle");
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView didEndEditingRowAtIndexPath");
}
There must be a simple thing I'm forgetting, but cannot figure out what. Any ideas?
You've implemented tableView:commitEditingStyle:, but the method you need is tableView:commitEditingStyle:forRowAtIndexPath:.
That's one of the challenges with implementing protocols. If you incorrectly specify or misspell optional methods, there's no way for the compiler to know.
Here are some tips that might help in the future:
Tip 1: Cut and paste protocol method declarations directly from the documentation to avoid spelling errors and missed arguments.
Tip 2: Create sets of correct protocol methods (with mostly empty bodies) and put them in a code snippet library. Then, always use the stored snippet when adopting a protocol.

Custom TableView Cell Indentation Issue

I created a custom UITableViewCell in my app and the default indent when the Delete button is present is not happening, below is a screenshot of my settings. Anyone have any idea as to what I'm doing wrong?
Also, here's a shot of the IB properties:
should happen by default, but in your UITableViewDelegate override
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
make sure you aren't doing anything strange in:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
and that you are setting the cell's editingAccessoryType to UITableViewCellEditingStyleDelete or UITableViewCellEditingStyleInsert
and that you aren't returning No from :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

reordering control in UITableView

I am developing a game in which I am using a UITableView which has custom cell (UItableViewCell subclass).
In editing mode:
Only the reordering control of the UITableView should show.
Right now I am getting the delete and the reordering control.
How to get only reordering control while editing ?
I know this answer might be late, but I'm putting it just for people who still need it.
Just implement the following methods:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
You should use this method for hiding delete button.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
thanks a lot oxigen it worked ....
i was writing this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.showsReorderControl = YES; // to show reordering control
return cell;
}
but i dint write the method which you have given
Thanks a tons
Swift 5.0
If you want to disable all other editing options for your tableView's cells except the one that allows you to reorder them then just implement those methods from UITableViewDelegate:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
true
}
// Removes the ability to delete current cell
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
.none
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
true
}
tableView.showsReorderControl = YES; // to show reordering control
To dissmiss delete control, in your UITableViewDelegate add
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}