OGeek|极客世界-中国程序员成长平台

标题: iphone - 在 UITableViewCell 中启用/禁用编辑模式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:05
标题: iphone - 在 UITableViewCell 中启用/禁用编辑模式

我有一个自定义 UITableViewCell,上面有 1 个文本字段和一个标签。我只想在表格 View 处于编辑模式时启用文本字段编辑。

我正在使用以下方法来启用文本字段编辑模式和禁用文本字段编辑模式。但这不起作用。我不确定这是否是正确的方法。如果这不是正确的方法,您能告诉我如何禁用启用文本字段吗?

- (NSIndexPath *)tableViewUITableView *)tableView willSelectRowAtIndexPathNSIndexPath *)indexPath {
    NSIndexPath *rowToSelect = indexPath;
    EditingTableViewCell *detSelCell;
    detSelCell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    detSelCell.textField.enabled = self.editing;

    // Only allow selection if editing.
    if (self.editing) 
    {
        return indexPath;
    }
    else 
    {
        return nil;
    } 
}

    - (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {

        if (!self.editing) 
        {
            return;
        }
        EditingTableViewCell *detcell;
        detcell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            detcell.selectionStyle = style;
            detcell.textField.enabled = self.editing;

我还有以下几行:

self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.

请帮忙!



Best Answer-推荐答案


---我找到了答案:

我已从以下方法中删除了启用/禁用代码:

- (NSIndexPath *)tableViewUITableView *)tableView willSelectRowAtIndexPathNSIndexPath *)indexPath {

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {

并在自定义 cell.m 中添加以下内容

- (void)setEditingBOOL)editing animatedBOOL)animated
{
    [super setEditing:editing animated:animated];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if(editing){
        textField.enabled = YES;
    }else{
       textField.enabled = NO;
    }

    [UIView commitAnimations];
}

它现在正在工作。我不确定这是否是正确的方法,但它工作正常。

关于iphone - 在 UITableViewCell 中启用/禁用编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9649986/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4