有没有办法在动态UITableView上使用UITextField,并在它们之间使用"tab"?



这可能有点长,但请耐心等待。它主要是简单的代码和日志输出。通常,如果我想将 UITextField 作为 UITableViewCell 的一部分,我可能会使用 a) 静态行或 b) 我将在情节提要中创建单元格,输出单元格并将字段输出到我的 ViewController,然后将单元格拖到"表视图"之外,但将其保留在场景中。

但是,我需要创建一个视图,在其中接受来自 28 种不同事物的输入。我不想输出28个不同的UITextField。

我想动态地执行此操作,以使其更容易。因此,我创建了一个带有标签和UITextField的自定义UITableViewCell。

我的视图控制器有两个数组。

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSArray *itemValues;

我的cellForRowAtIndexPath看起来像这样...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"ItemCell";
    MyItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[MyItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.categoryValue.tag = indexPath.row;
        cell.categoryValue.delegate = self;
    } 
    cell.item.text = [self.items objectAtIndex:indexPath.row];
    cell.itemValue.text = [self.itemValues objectAtIndex:indexPath.row];
    return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        NSInteger tag = [textField tag];
        NSLog(@"TFSR tag: %zd/%zd", tag, self.categories.count-1);
        if (tag < (self.categories.count - 1)) {
            NSIndexPath *nextIndex = [NSIndexPath indexPathForRow:tag+1 inSection:0];
            NSLog(@"TFSR nextRow: %zd/%zdn", nextIndex.row);
            FFFCategoryTableViewCell *cell = (MyItemTableViewCell *)[self.tableView cellForRowAtIndexPath:nextIndex];
            [self.tableView scrollToRowAtIndexPath:nextIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            [cell.categoryValue becomeFirstResponder];
        }
        else {
            NSLog(@"DONE!");
        }
    return YES;
}

事实证明,这是有问题的。目标是,用户应该能够在第一行选择UITextField,输入一个值,当他们点击键盘上的"下一步"键时,他们将被发送到第二行的UITextField。然后是第三、第四,...27日,28日。

但是,假设我首先突出显示 IndexPath.row = 11 的单元格上的 UITextField。如果我点击"下一步",这就是我的输出的样子......

======== OUTPUT ========
TFSR tag: 11/27
TFSR nextRow: 12/27
TFSR tag: 12/27
TFSR nextRow: 13/27
TFSR tag: 13/27
TFSR nextRow: 14/27
TFSR tag: 0/27
TFSR nextRow: 1/27

现在我完全明白为什么会发生这种情况。随着UITableView试图在加载各种单元格时节省内存,并使用dequeueReusableCellWithIdentifier...我只有 14 个单元格 (0-13)。然后它循环回到起点。

我的问题是...我不知道这个问题的解决方案。我希望用户能够在第 28 个 UITextField 行之前访问他的下一个。

关于如何实现这一目标的任何想法/解决方案?

问题是你对标签的使用。仅在首次创建单元格时设置标记。每次调用cellForRowAtIndexPath:都需要设置它。你想要:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"ItemCell";
    MyItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[MyItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.categoryValue.delegate = self;
    } 
    cell.categoryValue.tag = indexPath.row;
    cell.item.text = [self.items objectAtIndex:indexPath.row];
    cell.itemValue.text = [self.itemValues objectAtIndex:indexPath.row];
    return cell;
}

请注意,将标签设置为索引路径仅在行固定时才有效。如果您的表行更加动态(可以添加、删除或重新排序某些行),则使用索引路径作为标记将失败,必须使用另一种方法。

最新更新