繁体   English   中英

UITableView编辑模式

[英]UITableView Edit mode

我有UITableView ,我试图在编辑模式下默认加载它。 问题是当我这行table.editing=TRUE; 我的行消失了,我实现了这个方法:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
return UITableViewCellEditingStyleDelete;
}


 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
 {
 return NO;
 }

但没有运气。 我该怎么办?

正如Anish所指出的那样

[tableView setEditing: YES animated: YES]; 

但是你需要在viewWillAppear视图事件中使它才能使它工作。

试试这个...

[tableView setEditing: YES animated: YES];

在ViewDidLoad中写

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];

addORDoneRow

- (void)addORDoneRows
{
    if(self.editing)
    {
        [super setEditing:NO animated:NO];
        [_dbSongsTblView setEditing:NO animated:NO];
        [_dbSongsTblView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
    }
    else
    {
        [super setEditing:YES animated:YES];
        [_dbSongsTblView setEditing:YES animated:YES];
        [_dbSongsTblView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
    }
}

对于Row的MultipleSelection

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

注意:有三种编辑风格如下

UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert

注意:对于多个选择集,从属性检查器中选择和编辑样式为多个

要在编辑模式下加载tableView,您应该在viewDidLoad()调用setEditing(true, animated: false) viewDidLoad()

如果您的视图控制器是UITableViewController的子类,则无需更改,只需进行上述调用即可。 否则,如果您的视图控制器是UIViewController的子类,那么您应该以这种方式进行调用: tableView.setEditing(true, animated: true)

用Swift 2.2测试。

[self.tableView setEditing:!self.tableView.isEditing animated:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM