Custom UITableViewCell is not showing indent while editing - ios

I'm on iOS 7, have an UITableViewController, with a UITableView (linked up the data source and delegate) and trying to use editing. I use a custom UITableViewCell, with Dynamice Prototype in UIStoryboard. When I click on the edit button (self.editButtonItem), you see the name change to done, but the are no indent icons (- icon). Swiping from the cell works, I get a delete button.
Here is my code:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
All of my content is in the contentView

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
- (IBAction)editbtn:(id)sender {
if (self.tableView.editing) {
self.tableView.editing = NO;
[edit setTitle:#"Edit" forState:UIControlStateNormal];
}
else{
self.tableView.editing = YES;
[edit setTitle:#"Done" forState:UIControlStateNormal];
}
}
I think this code help you.

In a UIViewController self.editButtonItem doesn't work, you need to create a custom button.

Related

Objective-C: TableView Delete with didSelectRowAtIndexPath

In my TableView, I allow users to delete their posts using the swipe to delete:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
However, I also allow users to tap the cell to open up a detail view of the post:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// segue and stuff
}
The problem is, when the user swipes to delete, it also triggers the didSelect method and opens up the detail view. How can I make sure didSelectRowAtIndexPath only triggers on taps, while the delete triggers on a drag swipe?
You should have add the following delegate method as well:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
The whole implementation would be
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Based on your updated question, then I would add a edit button on the right top side of the tableview to inform user to delete the selected cell. Check the following threads
change Table to Edit mode and delete Rows inside a normal ViewController
iOS delete a tableview row
Here is also good tutorial, it is worth to check
http://www.appcoda.com/model-view-controller-delete-table-row-from-uitableview/
If you're using a UITableViewController + UIStoryboard and connect your detail view segue to push in your cell prototype selection outlet, this works as expected (didSelectRowAtIndexPath is not called when swiping):
#interface TableViewController ()
#property (strong, nonatomic) NSMutableArray *cellTitles;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.cellTitles = [#[#"Apple", #"Banana", #"Carrot", #"Millenium Falcon"] mutableCopy];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellTitles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
cell.textLabel.text = self.cellTitles[indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (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 {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.cellTitles removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

ios 9 UITableView freezes when deleting cell and does not show confirmation title

I have a code written in Objective-C for deleting cells from tableview.
Here's the code:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == (self.numberOfAddsShowing - 1) && (*moreAvailable)) {
return NO;
}
if (isEditable) {
return YES;
}
return NO;
}
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"حذف";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger rowNumber = indexPath.row;
if (rowNumber >= self.addsToShow.count) {
return;
}
HJAdd *adToDelete = [self.addsToShow objectAtIndex:rowNumber];
if (!self.adsToDelete) {
self.adsToDelete = [NSMutableArray array];
}
[self.adsToDelete
addObject:[[NSNumber numberWithInteger:adToDelete.addId] stringValue]];
[self.addsToShow removeObjectAtIndex:rowNumber];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[ indexPath ]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
[self setEditButtonState];
}
The problem is that when I enter edit mode and try deleting the cell, the confirmation title is not shown and I am unable to delete the cell. When I finish editing mode, the UITableView is no longer scrollable. Swipe to delete is also not working. When I swipe, the tableView enters edit mode but I cannot see the delete button.
When I run the app in Simulator with iOS 8.1 or lower, its working fine.
Here's a video I uploaded to Youtube to show the problem.
Any help would be appreciated. Thanks to all!
You don't need [tableView beginUpdates]; and [tableView endUpdates];.
I think these are for code outside of the commitEditingStyle method.
In the TableView Programming Guide these are not used.
if you have a button bar Item
ViewDidload
self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
TableAccountViewCell *accountCell = [tableView cellForRowAtIndexPath:indexPath];
if ([accountCell.myLabel.text isEqualToString:ADD_USER])
{
return NO;
}
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[menuItems removeObjectAtIndex:indexPath.row];
[_myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
//
// Start SQL Delele Account.
//
}
}

how to delete a row in UITableView in Xcode 6.2?

I wanna delete a particular row while I drag it.
Here is part of code.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableData removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
Do you set delegate ?
self.tableView.delegate = self;
With corresponding How do I get the delete button to show when swiping on a UITableViewCell?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// delete from model
// [self.tableView reloadData]
}
}
You can put on breakpoints into your code for check call functions
yagnesh dobariya's answer is a working solution, and I would like to propose a modification that makes it more efficient.
Since you are only deleting one row at a time, you can keep using -deleteRowsAtIndexPaths:withRowAnimation: as in your original code. Just sandwich it between -beginUpdates and -endUpdates.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}

delete UITableViewCell,depend on what section it is in

I got a problem: I have a 2 sections UITableView,i want the cells in section one can be delete(when swipe left show the delete),and the cells in section two cannot be delete.
somebody give me a hint ,thanks.
You can implement the tableView:canEditRowAtIndexPath: method from the UITableViewDataSource protocol to determine whether the row can be deleted or not...
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
return NO;
} else {
return YES;
}
}
Then, to handle the delete you will use the tableView:commitEditingStyle:forRowAtIndexPath: method...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check editing style and take action (delete or edit)
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Delete row %ld in section %ld", (long)indexPath.row, (long)indexPath.section);
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
I'm assuming you have already implemented the UITableViewDataSource delegate protocol, so it should only be a matter of adding these methods.

Grouped UITablleview not editable cell indentation

I am working on the app which contains one of the grouped section UITableView with edit/delete functionality. Tableview is looked like this
-(IBAction) EditTable:(id)sender
{
if (self.editing)
{
[super setEditing:NO animated:NO];
[libtable setEditing:NO animated:NO];
[libtable reloadData];
[tabledit setTitle:#"Edit" forState:UIControlStateNormal];
}
else
{
[super setEditing:YES animated:YES];
[libtable setEditing:YES animated:YES];
[libtable reloadData];
[tabledit setTitle:#"Done" forState:UIControlStateNormal];
}}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self delete];
}
}
On the very first row of each section I have disable the editing mode with this method:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0;}
It is working fine but you can see in this image :
The very first row of each section because of non-editable mode doesn't move to right side when I click on edit mode. I need these 0 index row also in right side on the click of edit button same like other rows. I didn't find any method to resolve this. If anyone has knowledge on this concept please help me out.
Thanks in advance.
Try like below
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) //for each section of first row return UITableViewCellEditingStyleNone
return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleDelete;
}
i forgot see your -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//you may comment this method
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; //return for every cell as editable
}
if u run and in editing mode u will get like this
try to use below delegate method to decide editing style for cells
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row ==0 ){
return UITableViewCellEditingStyleNone;
}else{
return UITableViewCellEditingStyleInsert;
}
}
and change your method canEditRowAtIndexPath to =>
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

Resources