1

我正在开发一个应用程序,其中我有一个由自定义 UITableViewCell 组成的 UITableView。每个 UITableViewCell 都有一个 UITextField。我想要做的是在填充所有 UITextFields 时启用 UIButton,我现在可以做到这一点。我现在的挑战是在没有填充所有 UITextFields 时禁用这个按钮。这是我正在使用的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    for (int i = 0; i < [self.table numberOfSections]; i++) {

        NSInteger rows =  [self.table numberOfRowsInSection:i];

        for (int row = 0; row < rows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            SimpleTableCell *cell = (SimpleTableCell *)[self.table cellForRowAtIndexPath:indexPath];

            NSNumber *numKey = [NSNumber numberWithInt: cell.dataField.tag];
            NSString *text = cell.dataField.text;

            if ([[cell dataField] text] != nil && [[[cell dataField] text] length] > 0) {

                [_dict setObject:text forKey:numKey];

                NSArray *keys = [_dict allKeys];

                if ([keys count] == ([_dataName count] - 1)) { //where _dataName is the array that is populating my UITableView

                    [_doneButton setEnabled:YES];
                    [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:28]];

                }

            } else { //if there is a UITextField that is empty

                [_dict removeObjectForKey:numKey];

                NSArray *keys = [_dict allKeys];
                //below is the clause where I disable the button, after checking if there is a single UITextField that is not populated
                if ([keys count] < ([_tireName count] - 1)) {

                    [_doneButton setEnabled:NO];
                    [_doneButton.titleLabel setFont:[UIFont systemFontOfSize:28]];

                }

            }

        }

    }

    return YES;
}

只有在填充了所有字段时才能启用按钮,这正是我想要的,但不幸的是,它仅在有三个空的 UITextFields 时才禁用按钮。我这样做的方法是:

当我浏览 UITableView 中的每个 UITextField 时,我会检查 UITextField 是否为空。如果它不为空,我将 UITextField 中的字符串值输入到 NSMutableDictionary 中,使用 UITextFields 标记值作为键。如果 UITextField 为空,那么我只需删除连接到该键的对象。从理论上讲,我的方法是有道理的,但它并没有像我希望的那样工作。谁能看到我做错了什么?

4

1 回答 1

1

当调用 textField:shouldChangeCharactersInRange:replacementString: 时,textField 的文本值尚未更改。此方法用于在文本实际更改之前确定是否应允许用户输入的文本。因此,当您检查文本是否为空时,正在编辑的文本字段会说它不是空的,即使它即将变为空。

换句话说,如果您正在编辑一个包含“A”的文本字段,并且用户按下退格键,当您在此方法中检查文本时,它会说它是“A”,而不是它“”。因此,当您清除其中一个文本字段的文本时,这将永远不会禁用该按钮。

从第二个文本字段中删除文本时,它应该捕获对前一个文本字段的更改,但是您if ([keys count] < ([_tireName count] - 1))正在if([keys count] < [_tireName count])禁用if([keys count] == [_dataName count])按钮总是比你的字典多 1 条记录。

因为当您的字典中的键少于 [_tireName count] -1 个键时,您正在禁用该按钮,所以除非您有 2 个空文本字段,否则您似乎永远不会禁用该按钮。

一旦你清除了第二个文本字段,一旦你编辑了第三个,它就会认为是时候让你最终禁用该按钮了。

你可以尝试改变

 NSString *text = cell.dataField.text;

更像这样的东西

NSString* text;
if(textfield == cell.dataField) //if this cell's textfield is the field we are editing
{
    text = [cell.dataField.text stringByReplacingCharactersInRange:range withString:string];
}
else
{
    text = cell.dataField.text;
}

这将阻止您在进行验证时查看正在编辑的文本字段的旧值。并更改if([keys count] < (_tireName count] -1))if([keys count] < [_tireName count]).

可能还有一种方法可以更改它,这样您就不需要遍历 tableview 中的所有行,但是如果您的 tableview 中只有 8 行,那么这种更改将不会有什么好处。

于 2013-09-11T17:26:03.950 回答