【问题标题】:UITableview section editableUITableview 部分可编辑
【发布时间】:2011-07-07 13:06:14
【问题描述】:

我的表格视图中有 5 个部分 1)1 和 2 不可编辑,而 3,4 和 5 可编辑 在第 1 节中使用复选框,2 单选框,3 插入单元格,4 删除单元格,5 移动单元格

所以对于最后 3 个,我希望我的 tableview 可以编辑,并且 + 和 - 的符号将显示在单元格的开头。

而不是 forst 2 部分。 我试过 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

但这不起作用。有什么帮助吗?

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
     {
     // Return NO if you do not want the specified item to be editable.
    
      NSInteger section = [indexPath section];
      if (section ==0)
        return NO;
      if (section ==1)
        return NO;
      if (section ==2)
        return YES;
      if (section ==3)
        return YES;
      if (section ==4)
        return YES;
    
    
     }
    
    
    
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:animated];
        [tableView reloadData];
    }
    

    【讨论】:

    • 是的,但我必须指定 setEdting: 方法?
    • 不需要检查一切是否正常。但无论如何我已经粘贴了它的代码。
    • 它显示所有部分不可编辑,如果我写 [myTableView setEditing:YES animated:YES]; [myTableView 重载数据];在 viewdidload 然后它显示所有可编辑的,在后一种情况下,我没有调用 didSelectRowAtIndexPath 方法
    【解决方案2】:

    tableView:canEditRowAtIndexPath: 让您走在正确的轨道上。您还需要实现tableView:editingStyleForRowAtIndexPath:

    另外,您是否在 UITableView 上调用 setEditing:animated 以将表格置于编辑模式?我假设你是,但仔细检查永远不会有坏处。

    【讨论】:

    • 它是针对整个表格视图而不是针对特定部分
    • @PJR - 当您希望表格进入编辑模式时,从您的UIViewController 调用它。如果您只是想对其进行测试,请从您的 viewDidLoad 方法中调用它。是的,它适用于整个 tableview。这就是为什么你实现tableView:canEditRowAtIndexPath: 来为你不想编辑的任何部分返回NO
    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2023-04-08
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2015-10-25
    • 2011-05-24
    • 1970-01-01
    相关资源
    最近更新 更多