【问题标题】:UITableView Edit modeUITableView 编辑模式
【发布时间】:2011-05-14 12:33:32
【问题描述】:

我有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;
 }

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

【问题讨论】:

    标签: ios iphone uitableview editmode


    【解决方案1】:

    作为 Anish 指出使用

    [tableView setEditing: YES animated: YES]; 
    

    但您需要在viewWillAppear 视图事件中使用它才能使其工作。

    【讨论】:

    • 是的,这有效,但我的右键消失了。我怎么能阻止它。
    • 你的意思是self.navigationItem.rightBarButtonItem?你为什么不尝试在viewWillAppear 中添加它?
    • 不。我的意思是我有一个按钮,你可以移动到另一个页面。
    • 正常模式下存在。我是说附件按钮。
    • 你在cellForRowAtIndexPath代理中添加[cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;];吗?
    【解决方案2】:

    试试这个...

    [tableView setEditing: YES animated: YES];
    

    【讨论】:

    【解决方案3】:

    在 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];
        }
    }
    

    对于多行选择

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

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

    UITableViewCellEditingStyleNone,
    UITableViewCellEditingStyleDelete,
    UITableViewCellEditingStyleInsert
    

    注意:对于多重选择,从属性检查器将选择和编辑样式设置为多重

    【讨论】:

    • 有理由打电话给reloadData吗?在我的情况下,它不是,除了在进入/退出编辑模式时禁用动画
    【解决方案4】:

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

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

    使用 Swift 2.2 测试。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2011-06-05
        • 2014-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 2011-01-19
        相关资源
        最近更新 更多