我正在开发一个应用程序,其中我有一个由自定义 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 为空,那么我只需删除连接到该键的对象。从理论上讲,我的方法是有道理的,但它并没有像我希望的那样工作。谁能看到我做错了什么?