Delete button disappeared on scrolling in edit mode of UITableview - ios

I am facing the issue when I scroll the tableview in edit mode. It's because of reusability of cells. I followed the link-
UITableView in Edit Mode - Delete Button Dissapears on Scroll
Got nothing from that.
Here is my code snippet-
- (void)setEditing:(BOOL)editing
animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
[tableView setAllowsMultipleSelectionDuringEditing:editing];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
[cell setEditingAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.textLabel.text=[NSString stringWithFormat:#"%ld",indexPath.row+1];
cell.detailTextLabel.text=#"";
cell.clipsToBounds = YES;
return cell;
}
So any recommendation or suggestion from anyone how to resolve that issue?
Thank you.

Change -(void)setEditing:(BOOL)editing animated:(BOOL)animated method into this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
_myTV.allowsMultipleSelectionDuringEditing = NO;
[super setEditing:editing animated:animated];
if(editing) {
[_myTV setEditing:YES animated:YES];
} else {
[_myTV setEditing:NO animated:YES];
}
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; // return yes
}
Now run your program and let me know what happening?
edit
Get this demo from here. Nice exlanation is given

Related

I have created a table view programmatically as follows-

- (void)viewDidLoad {
[super viewDidLoad];
menuArray=[[NSMutableArray alloc]initWithObjects:#"Address",#"Restaurants",#"Deals",#"Orders",#"Account",#"Address Book",#"Settings",#"Live Chat",#"Info",nil];
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, menuSubView.frame.size.height, self.view.frame.size.width-80, self.view.frame.size.height-menuSubView.frame.size.height)];
tableView.delegate=self;
tableView.dataSource=self;
[self.view addSubview:tableView];
}
and other methods as follows-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return menuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reusableIdentifier=#"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableIdentifier];
}
cell.textLabel.text=[menuArray objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
However when I run the program i am able to see the table view as desired. But when I scroll through it, table's data disappear and only blank table view remains. Can anyone help me? Thanks in advance.
--> I appreciate above answer.
--> Please try this line in "cellForRowAtIndexPath" Method...!
--> Sometimes it happens. I have solved this issue with this line in past. I hope it's useful for you.
UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:#"Cell4" forIndexPath:indexPath];

iOS app - delete buttons not showing when table view is in editing mode

I am very new to iOS but I've created a view controller with a table view which i want to put into editing mode. This is my view controller implementation file code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize progsTable, programmes;
- (void)viewDidLoad
{
[super viewDidLoad];
programmes = [[NSMutableArray alloc] initWithObjects:#"one",#"two",#"three",nil];
//set the title
self.title = #"Programmes";
//add an edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[progsTable setEditing:editing animated:animated];
}
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return programmes.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [programmes objectAtIndex: indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete) {
//remove from array
[programmes removeObjectAtIndex:indexPath.row];
//remove from table
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - UITableView Delegate methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#end
I get the edit button in the top left but when i click it the red delete buttons don't appear - what am I doing wrong?
Running your exact code does work - the delete buttons are displaying when i test it.
I believe the problem is that you haven't assigned your #property called progsTable to your Table View.
To solve it do the following:
Go to your Storyboard
Right click on your View Controller
Click and hold the + next to the outlet called progsTable while you drag the mouse to your Table View, like this:
Try running your app - the delete buttons should now appear.
I think you need to add the code below:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete; }
This tells the UITableView which editing style you want.
Use this
- (IBAction)deleteDrug:(id)sender event:(id)event {
selectedButtonIndex = [self indexPathForButton:sender event:event];
[tableView setEditing:YES animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == selectedButtonIndex) {
return YES;
}
return NO;
}
You may need to implement this method for your datasource.
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
Check that you also have auto-layout constraints added in interface builder so that the very end of the cell lines up with the width of your device.
Run the code on the ipad in the simulator, if the delete button shows up there but not on the iphone simulator then it probably just that the contents are not aligned properly to the viewport of your device.
My 'delete' button was not showing up but the cell title content were moving over to the left when I swiped, however I had not set constraints on my cell width in my storyboard, as soon as I set the right-edge constraint to '0' or '16', the delete button appeared.

How to move uitableview custom cell?

I use custom cell.I want to move this cell (change position of the cells)
i used this delegations
UITableViewDataSource,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Cellidentifier = #"Celledit";
CellEdit *editCell =[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (!editCell) {
editCell = [[CellEdit alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}
MItem *mItem = [mItems objectAtIndex:[indexPath row]];
---------------------------------
// set data 3 labales in the cell
---------------------------------
editCell.editdelegate = self;
return editCell;
editcell is my custom cell
[_tableViewEdit setEditing:YES animated:YES]; this line put in - (void)viewDidLoad
i want to edit it always
// table view change cells
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destRow = destinationIndexPath.row;
id object = [minutesItems objectAtIndex:sourceRow];
[minutesItems removeObjectAtIndex:sourceRow];
[minutesItems insertObject:object atIndex:destRow];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
for (UIControl *control in cell.subviews)
{
if ([control isMemberOfClass:NSClassFromString(#"UITableViewCellReorderControl")] && [control.subviews count] > 0)
{
for (UIControl *someObj in control.subviews)
{
if ([someObj isMemberOfClass:[UIImageView class]])
{
UIImage *img = [UIImage imageNamed:#"logocoffee.png"];
((UIImageView*)someObj).frame = CGRectMake(0.0, 0.0, 43.0, 43.0);
((UIImageView*)someObj).image = img;
}
}
}
}
}
}
to move the cell simulator not showing right side control icon
why this?
that means cant move costume cells?
Try this code,
You should use below two method,
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Use this method for moving the row
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
[label1array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
[label2array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
[label3array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}
Custom cells can be moved as well.
From your code samples we don't see where you are setting your tableview in the editing mode.
Perhaps the issue is because you are never setting the editing mode.
If so, try to add an Edit button in the title bar for example, which should be linked to a method such as:
- (IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[yourTableView setEditing:NO animated:NO];
[yourTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
}
else
{
[super setEditing:YES animated:YES];
[yourTableView setEditing:YES animated:YES];
[yourTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
}
}
I think this one should resolve your issue.
Edit: To resume in your case, as a minimum, you only need to set your table view in the editing mode in viewDidLoad, and to update it if needed:
[yourTableView setEditing:YES animated:YES];
[yourTableView reloadData];
and to implement both callbacks:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//Even if the method is empty you should be seeing both rearrangement icon and animation.
}
I tried with custom cells and it's working fine.

iOS edit table view not working

I'm fairly new to this and am stuck on a problem. I can't get the ability to edit a table view i've created...
I have a button on the top right corner of the app that says "Edit" and - (IBAction)editTable:(id)sender, I've tried numerous attempts to get to edit this list I've created...
#implementation XYZViewController
#synthesize animalNames;
- (void)viewDidLoad
{
[super viewDidLoad];
//create array called animal names
animalNames = [[NSArray alloc]initWithObjects:
#"Cow",
#"Dog",
#"Cat",
#"Dear",
#"Penguin",
#"Lion",
#"Leapord",
#"Eal",
#"Snake",
#"Moth",
#"Cheetah",
#"Turtle"
, nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.animalNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"MainCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [self.animalNames objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)editTable:(id)sender
{
NSLog(#"Editing");
[super setEditing:TRUE];
[self.tableView setEditing:TRUE];
self.editing = YES;
}
#end
Using Xcode 5.
You need to implement following methods to support table view editing:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
You should declare animalNames as NSMutableArray as you are going edit it. Then you need to override some delegate methods to perform edit in tableview.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *sourceItem = _animalNames[sourceIndexPath.row];
[_animalNames removeObjectAtIndex:sourceIndexPath.row];
[_animalNames insertObject:sourceItem atIndex:destinationIndexPath.row];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
[_animalNames removeObjectAtIndex:indexPath.row];
[_tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
You can stop editing in editTable method like
- (IBAction)editTable:(id)sender
UIBarButtonItem *button = (UIBarButtonItem *)sender;
if ([button.title isEqualToString:#"Edit"]) {
button.title = #"Done";
[self.tableView setEditing:TRUE];
} else {
button.title = #"Edit";
[self.tableView setEditing:NO];
}
}
Hope this will help you
To edit the table view you need to call
[self.tableView setEditing:YES animated:YES]; on the table which you are using then
You have to implement the delegate method
tableView:commitEditingStyle:forRowAtIndexPath:
method to enable the deleting of rows. In order to delete the rows you need to use
Then Do this..
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Remove the object at the index we need to delete
[YourTableArray removeObjectAtIndex:indexPath.row];
//Delete the rows at the Indexpath.
[YourTable deleteRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
[reminder_table reloadData];
//Set the table to editing NO.
[YourTable setEditing:NO animated:YES];
}
Please find below link for your reference
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19

When I press the button, I want the content of the text field to be added- IOS TableView

i have updated my code,
everything seems good, but when i press the add button it does not add the table.
then i realized i have forgot to initialize array, then everything is ok
#import "RootViewController.h"
#import "Hello_WorldAppDelegate.h"
#implementation RootViewController
#synthesize tableView;
- (void)viewDidLoad {
// Add the following line if you want the list to be editable
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (IBAction)addToTable:(id)sender{
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Do you want to say hello?" message:#"More info..." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Say Hello",nil];
[alert show];
[alert release];
*/
[myArray addObject:textField.text];
[self.tableView reloadData];
textField.text=#"";
/*
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
[cell setText:[textField text]];
[self.tableView reloadData];
*/
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return[myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
[cell setText:[myArray objectAtIndex:indexPath.row]];
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
}
// Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
/*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
}*/
}
/*
Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
}
- (void)viewDidDisappear:(BOOL)animated {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[textField release];
[addButton release];
[super dealloc];
}
- (void)viewDidUnload {
[textField release];
textField = nil;
[addButton release];
addButton = nil;
[super viewDidUnload];
}
#end
ok i have solved my issue
thanks to everyone
Actually the problem is with your method name.
- (IBAction)addToTable:(id)sender:(UITableView *)tableView
change it like this
- (IBAction)addToTable:(id)sender
SWIFT(3.0)
#IBAction func add(_ sender: Any) {
yourarrayname.append(yourtextfield.text!)
yourtableview.reloadData()
}
what you need, first use an array as a data source for a table then on click on the button you need to add the textField content into an array then reload the table.
so your button action look like
- (IBAction)addToTable:(id)sender
{
[dataSouceArray addObject:yourTextField.text];
[yourTable reloadData];
yourTextField.text=#"";
}
and tableViewDelegate look like
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourDataSourceArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
[cell setText:[yourDataSourceArray objectAtIndex:indexPath.row]];
// Set up the cell
return cell;
}

Resources