代码之家  ›  专栏  ›  技术社区  ›  Shawn

无法在UITableView中获取插入控件

  •  1
  • Shawn  · 技术社区  · 15 年前

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing:editing animated:animated];
        [categoryTV setEditing: editing animated:animated];
    
        NSIndexPath *ip = [NSIndexPath indexPathForRow:[[appDelegate appCategories] count] inSection:0];
    
        [self.categoryTV beginUpdates];
            if (editing) {
                [categoryTV insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationLeft];
            } else {
                [categoryTV deleteRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationFade];          
            }
        [self.categoryTV endUpdates];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     ...
    
        if (indexPath.row >= [[appDelegate appCategories] count])
            cell.textLabel.text = NSLocalizedString(@"New Category", @"NewCategoryCellText");
        else
            cell.textLabel.text = [[[appDelegate appCategories] objectAtIndex:indexPath.row] detailValue];
    
    
        ....
    
        return cell;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (self.editing) {
            return [[appDelegate appCategories] count] + 1;
        } else {
            return [[appDelegate appCategories] count];
        }
    }
    

    如前所述,我忘了包括我的建议方法版本,现在显示如下。谢谢

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == [[appDelegate appCategories] count]) {
            return UITableViewCellEditingStyleInsert;
        } else {
            return UITableViewCellEditingStyleDelete;
        }
    }
    
    3 回复  |  直到 15 年前
        1
  •  3
  •   rickharrison    15 年前

    您需要实现以下委托方法来指定编辑样式:

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

    显然,您将只为希望具有插入控件的单元格返回该值。否则,您需要返回 UITableViewCellEditingStyleDelete .

        2
  •  1
  •   nhahtdh Pankaj Wadhwa    12 年前

    我在我的 UITableView ,问题是我的 canEditRowAtIndexPath NO 对于插入控制行。

        3
  •  1
  •   Vineet Choudhary    10 年前

    设置 tableview

    - (void)viewDidLoad {
        .......
        self.tableView.editing = YES;   //set editing property of tableview
        .......
    }
    

    编辑

    公告

    @属性(非原子,getter=isEditing)布尔编辑

    当此属性的值为“是”时,表格视图处于编辑状态 模式:表格的单元格可能显示插入或删除 每个单元格左侧的控件和 右侧,取决于单元的配置方式。(参见 有关详细信息,请参阅UITableViewCell类参考。)点击控件导致 调用数据源方法的表视图 tableView:commitEditingStyle:forRowAtIndexPath:. 默认值为否。

    然后返回要在其中插入控件的单元格

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