iphone - 在 UITableViewCell 中启用/禁用编辑模式

标签 iphone ios uitableview

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

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

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)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.

请帮忙!

最佳答案

---我找到了答案:

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

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

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

- (void)setEditing:(BOOL)editing animated:(BOOL)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/

相关文章:

ios - 从模态视图 Controller 返回时标签栏消失

ios - swift fatal error : unexpectedly found nil while unwrapping an Optional value (lldb) in Tableview

iphone - 首先导航,然后在 iPhone 应用程序中调用 viewDidLoad

ios - 通过 UIViewController 传递触摸

iphone - 隐藏 UIStatusBar/移动 UINavigationBar

ios - 将 NSArray 传递到 tableview swift 3.0

iphone - 为 UIWebView 添加模型层(带缓存); UIWebViewNavigationType 和 shouldStartLoadWithRequest 的问题

iphone - 如何在 iPhone 应用程序中使用 Dropbox Api 共享文档?

ios - 您可以从一个类发送消息以触发另一个类中的 'self' 吗?

objective-c - 在需要或不需要实现协议(protocol)的情况下进行委托(delegate)……背后的原理是什么?