如何以编程方式调用UITableView的编辑属性

oky*_*eni 2 iphone uitableview uiviewcontroller

我在我的应用程序上获得了一个UITableView而不使用NIB.但是,因为我这样做,我无法获得通常与常规UITableView相关联的编辑属性.例如,如果我改为使用@interface TableViewController:UITableViewContoller并在导航栏上添加editButtonItem,则在按下该按钮后将自动包含删除和移动行.

但是,我的UITableView没有任何作用.请帮忙.

// 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];
    }
Run Code Online (Sandbox Code Playgroud)

然后是一堆委托方法,如:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath,
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

等等...

我不想详细介绍委托方法,因为我使用简单的UITableViewController创建了一个新项目,并且它可以工作.

顺便说一下,当我运行代码时,我设置断点并调用委托方法但没有任何反应.它没有给我单元格左侧的删除图标,右侧没有移动单元格图标.什么都没发生.

非常感谢!!!

Ole*_*ann 5

您必须调用-[UITableView setEditing:animated:]才能使表格视图进入和退出编辑模式.