I got a UITableView on my app without using a NIB. However, because I do it this way, I can't get the editing properties usually associated with a regular UITableView. For example, if I instead use @interface TableViewController:UITableViewContoller and add a editButtonItem on the navigation bar, the delete and moving rows will automatica开发者_开发技巧lly be included once I press on that button.
However, nothing works on my UITableView. Please help.
// in .h
@interface TableViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
UITableView *tView;
NSMutableArray *aMutArray;
}
// in .m
-(id)init
{
[super initWithNibName:nil bundle:nil];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
[[self navigationItem] setTitle:@"ReorderingTableCell"];
return self;
}
- (void)loadView
{
[super loadView];
CGRect tableFrame = CGRectMake(0, 0, 320, 300);
tView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
[[self tView] setDelegate:self];
[[self tView] setDataSource:self];
aMutArray = [[NSMutableArray alloc] initWithObjects:@"first", @"second", @"third", nil];
[[self view] addSubview:tView];
}
and then a bunch of delegate methods like:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath,
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
etc...
I don't want to go through the details of the delegate methods because I create a new project with a simple UITableViewController and it works.
BTW, when I run the code, I set breakpoints and the delegate methods are called but nothing happens. It does not give me the delete icon on the left of the cell and no moving cell icon on the right side. Nothing happens.
Thanks so much!!!
You have to call -[UITableView setEditing:animated:]
to bring the table view in and out of edit mode.
精彩评论