Reputation: 3908
I have a situation whereby I have a ViewController that contains an edit button and within that another View (which is separate) contains my tableview.
I am currently adding the edit button programmatically (as you would with a single view). However as expected when you tap the edit button it changes to 'cancel' yet the table does not go into edit mode (all the normal methods are enabled and contain the code that I would use for a single view).
I am therefore wondering how do I send a 'message' from my edit button in the View controller to the table view (which as mentioned is separate). Do I need to set up a delegate? or is there a special method I can call ?
I have googled for this a fair bit but if anyone could help even point me in the right direction I would really appreciate it.
Upvotes: 2
Views: 273
Reputation: 7758
I'm going to refer to the controller which owns the edit button as VC1, and the controller which owns (is the delegate for) the table view as VC2. Also, read up on Delegation. It's not an optional subject in iOS development.
In VC2, declare a method named -setTableIsEditing:(BOOL)isEditing, and implement it to simply set the isEditing property on your table view, like so:
- (void)setTableIsEditing:(BOOL)isEditing {
self.myTableView.isEditing = isEditing;
}
Then in VC1's implementation of the button touchUpInside delegate, update an ivar bool to track the editing mode, and call that method on VC2 with the correct parameter:
- (IBAction)editButtonPressed {
_isEditingTable = !_isEditingTable;
[self.myVC2Instance setTableIsEditing:_isEditingTable];
}
Upvotes: 1
Reputation: 12325
Use can use
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
And then implement
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
// You can use NSUserDefaults for sending information to the controllers in another view
//and you can manipulate them.
}
Upvotes: 0
Reputation: 3038
A delegate would work or you can send an NSNotification from the button touched method to which the table view would subscribe. Then in there you call [UITableViewCell setEditing:YES animated:YES] for each cell as appropriate.
Upvotes: 0