I am building an iPhone application that is using a table cell. I set up this code to try to delete cells from the table, but it does not seem to work because I am not using a NSMutableArray. When I swipe I get the delete button but the delete button does not do anything. My question is if I am not using NSMutableArray with my table cell ho can this line: [self.dataArray removeObjectAtIndex:indexPath.row]; work?
Code Used
(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES; }
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
[self.dataArray removeObjectAtIndex:indexPath.row];
} }
Here is the link to the apple doc's on table view programming guide. Table view programming guide It has a easy delegate method that uses NSArray and has an example code you can use for deleting and inserting. The code you have shown above is missing the delegate part in order to perform the delete. Take a look at listing 7.3 that maybe what your looking for.
Related
I want to delete a row in my table view cell. At the moment, the code will look like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// Remove the row from data model
[listOfAllOpenChats removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
The problem: I will become an error:
No visible #infterface "NSArray" declares the selector
"removeObjectAtIndex:"
Replace your NSArray listOfAllOpenChats with NSMutableArray,you cannot remote object from NSArray
Your listOfAllOpenChats is probably a NSArray type, which has NOT support to remove or add objects. Change your listOfAllOpenChats array type to NSMutableArray and your problem will be gone.
You can't add or remove objects from an NSArray(immutable). You have to use an NSMutableArray.
I want to delete a row from my tableview. I cannot find a solution that works from the other simialr questions here.
I know I need to use the following code, But I cant seem to figure out how to properly finish this line of code to make it work.
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
But what goes where is says "dailyIncomeArray"?
[what-goes-here removeObjectAtIndex:indexPath.row];
I have a getData method that populates my tableview. Next I want to be able to swipe to the left and hit that red delete button. Thanks in advance for your help!
Heres the rest of the method
// 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
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
//[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:results] 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
}
}
Here's the error I'm receiving. I don't understand what this means.
No visible #interface for 'NSArray' declares the selector 'removeObjectAtIndex:'
I want to delete rows in a UITableView, I need to provide the delete button not by swipe but in a way similar to delete messages functionality in iPhones. In which on clicking edit a small red circle appears on the left in the cell which rotates when clicked and the delete button is shown. How to implement this?
Like in the image below:
See the Apple documentation here for details on how to achieve this. In particular:
When sent a setEditing:animated: message (with a first parameter of
YES), the table view enters into editing mode where it shows the
editing or reordering controls of each visible row, depending on the
editingStyle of each associated UITableViewCell. Clicking on the
insertion or deletion control causes the data source to receive a
tableView:commitEditingStyle:forRowAtIndexPath: message. You commit
a deletion or insertion by calling
deleteRowsAtIndexPaths:withRowAnimation: or
insertRowsAtIndexPaths:withRowAnimation:, as appropriate.
write this self.navigationItem.leftBarButtonItem = self.editButtonItem; in viewDidLoad method. After that implement this method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[yourArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
I have a tableview that loads with isEditing = YES. This is a bit of a hack (but a reasonable one I think) because I want one of my cells to always display the 'add' icon. I don't want the other cells to include the 'delete' icon so I am returning UITableViewCellEditingStyleNone for these.
However, I would still like to provide swipe to delete functionality on those cells. Since the tableview is already in edit mode, is this going to be possible?
If not, I will probably achieve the 'add' icon using the cells imageView, but this seems hacky too.
Edit: Similar question found here:
swipe to delete when already in edit mode
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[myArray removeObjectFromindex];
}
[mytableview reloadData];
}
I am making an app that has a basic tableview where users can add data to the table, just for within their own version of the app. Needs to be on the iPhone alone, so no internet connection to a database etc.
Now having a look at the native notepad app it looks like that is what I am after - so when the user presses a + button, it then goes to another UIView and the user can enter their data and then the title of the or the first line is set in the table view.
Is there a tutorial for this, or in what area am I suppose to look for this?
At the moment, I can add a hardcoded string into the app by using this code, which i assume is the bit I need to mofidy:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[arryData removeObjectAtIndex:indexPath.row];
[tblSimpleTable reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
[arryData insertObject:#"Mac Mini" atIndex:[arryData count]];
[tblSimpleTable beginUpdates];
[tblSimpleTable endUpdates];
}
}
Am I on the right track here?
Cheers
-Jeff
On -viewDidDisappear in the new UIViewController, you could have it update an NSArray that would get passed between the views with a delegate method. You could store an array filled with the strings of the titles, then just has the tableView update when -viewWillAppear is called.