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.
Related
In my app I have a Table view with values from a mapkit. I can edit the table, delete the row, but when the app starts over, the same deleted rows are still there....How can I fix this? The code for the editing:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_locations removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
Any tips? Thank you
Load the data from core data using a NSFetchedResultsController.
Implement the delegate function to update your views when change occur.
When you delete something delete enqueue a block to delete it from core data. (if you are using NSPersistentContainer use performBackgroundTask:).
Don't update your views. This will happen automatically because of the delegate callback that you implemented in step 2.
I need to leave delete mode after user tapped Delete button. I want to show some activity indicator and wait the server response on delete action before I actually remove the cell (or not if the server does not respond). This action I want to perform from delegate method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
How can I do it?
To hide the "Delete"-Button, you need to use the setEditing:animated: method.
However, your implementation of tableView:commitEditingStyle:forRowAtIndexPath: needs to execute this with a slight delay. The note in the reference Table View Programming Guide for iOS underneath Figure 7-1 states:
Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.
So your implementation could be like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove delete button only after short delay
[self performSelector:#selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
}
}
- (void)hideDeleteButton:(id)obj
{
[self.tableView setEditing:NO animated:YES];
}
The above code makes the Delete-Button slide away after 0.1 seconds when the user presses it. You will then, of course, need to prevent it going back into the editing mode while waiting for the action to complete. For that, you can override the UITableViewDataSource method tableView:canEditRowAtIndexPath: to prevent the same cell or the whole table to be edited again while waiting.
In my code I make a call to
[self.tableView reloadData];
I'm not sure if this is the 100% correct way to do it but it works for me. So your function could be something like:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your processing.
[self.tableView reloadData];
}
It should then clear out the red Delete Button and refresh your data.
You may also be able to play around with the call
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
but in my experience its the reloadData call that seems to do the trick
Hi Let me get it straight: Your datasource is actually online and you want confirmation that it is deleted before you update your tableview - in the mean time you want to display an AI Activity indicator.
If that is what you want to do then you start with this method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get the key of the data you want to delete.
data_struct_instance * data = [self.mutableArray_data_source objectForRowAtIndexPath:indexPath];
NSString *data_key = data.key;
row_index = indexPath.row; // set a property to preserve the row (or index path for multilevel data) for use when you delete the the record later.
[self start_UIActivityIndicator];
[self callonline_with_delete_command:data_key]; // send the request to delete the record
}
once the server responds depending on whether it is successful or not you can either delete the record or reload the entire array if the table is small this is preferable to ensure that the data is synced -
....
[activityindicator StopAnimating];
if (success) {
[self.mutableArray_data_source removeObjectAtIndex:row_index];}
else {
NSLog ....
}
[self.tableView reloadData]; // resets the delete button.
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 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.
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];
}
}