【问题标题】:custom editingAccessoryView not working自定义编辑AccessoryView 不起作用
【发布时间】:2011-09-03 20:56:16
【问题描述】:

对于带有自定义单元格的 UITableView,我有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FolderCellViewController"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FolderCellViewController" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
        cell.editingAccessoryView=accessoryView; //accessoryView is a UIView within a UITableViewCell, and it is properly connected in IB
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return NO; //YES here makes a red delete button appear when I swipe
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

但是对于某些人来说,当我滑动时什么都没有发生。除了这个,我什么都没做——我还需要做什么才能让它工作吗?

编辑:显然我所做的只是为整个表格处于编辑模式时设置编辑样式,而不是在我在每个单独的单元格上滑动时设置编辑样式。所以我想要做的是,当我在每个单元格上滑动时,该单元格的自定义附件视图会出现。但我不知道该怎么做..

【问题讨论】:

    标签: ios uitableview uikit


    【解决方案1】:

    当单元格进入编辑模式时,会显示编辑附件视图。真正让这个工作似乎有点太难了,但我已经做到了:

    为了在进入整个表格的编辑模式时以及在滑动单个行时显示此内容,我在 UITableViewController 子类中实现了以下内容:

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
        if (editing)
            self.editingFromEditButton = YES;
        [super setEditing:(BOOL)editing animated:(BOOL)animated];
        self.editingFromEditButton = NO;
        // Other code you may want at this point...
    }
    

    editingFromEditButton 是子类的 BOOL 属性。当标准的“编辑”按钮被按下时,这个方法被调用。它用于以下防止标准删除按钮显示的方法:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (self.editingFromEditButton)
            return UITableViewCellEditingStyleNone;
    
        // Otherwise, we are at swipe to delete
        [[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES];
        return UITableViewCellEditingStyleNone;
    } 
    

    如果整个表格视图被设置为编辑模式,那么每个单元格也将被发送 setEditing 消息。如果我们滑动了一行,那么我们需要强制该单元格进入编辑模式,然后返回UITableViewCellEditingStyleNone 样式以防止出现标准删除按钮。

    然后,要关闭自定义编辑附件,您还需要以下代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        // Cancel the delete button if we are in swipe to edit mode
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.editing && !self.editing)
        {
            [cell setEditing:NO animated:YES];
            return;
        }
    
        // Your standard code for when the row really is selected...
    }
    

    【讨论】:

    • 如果您将[[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES]; 更改为[[tableView cellForRowAtIndexPath:indexPath] setEditing:!cell.isEditing animated:YES];,您将获得滑动关闭的额外好处,与默认删除按钮的工作方式相同。
    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2013-08-28
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多