8

I am writing an IPhone IM chat window now.

You know if the nagviationItem.rightBarItem in an UITableviewController is set to editButtonItem, you click the edit button then a red delete icon is shown to every single row, like shown here.

The problem is, I have a UIViewController instead of a UITableViewController:

@interface ChatUIViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
    UITableView *table;
    UITextField *textField;

}

and i did same to set editButtonItem:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

when i click the edit button, the red delete icon will not show.

How can I make the red delete icon shown in my ChatUIViewController?

Thanks in advance to anyone who might help. Michael

Community
  • 1
  • 1
Michael Z
  • 4,534
  • 4
  • 21
  • 27

1 Answers1

39

Add the following method to your table view delegate class:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    if (editing) {
        // you might disable other widgets here... (optional)
    } else {
        // re-enable disabled widgets (optional)
    }
}
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Thanks a zillion, it worked perfectly. The solution leads another question: Why the above code is not needed in a class that is UITableViewController class, but is needed in a class that subclasses the UIViewController? My intuition tells me the UITableViewController class calls setEditing:animated: under the hood. – Michael Z Jan 20 '10 at 20:34
  • 1
    My opinion is that a table view is not necessarily the only editable view. By making it part of a view controller, one can pass editable calls to any subview (like a table view). – Alex Reynolds Jan 20 '10 at 21:29