【问题标题】:how to move to next cell/row in table view with textview如何使用textview移动到表格视图中的下一个单元格/行
【发布时间】:2013-01-29 12:29:39
【问题描述】:

我被困在这个问题上。请帮助我解决这个问题。 这是我的图像,现在我在表中有 5 个不同的行。 如果您在编辑第一个字段后单击第一个文本字段,那么我想要完成按钮单击转到第二个字段并同时打开文本视图和文本字段(如图所示)。 类别归档有选择器视图,名称字段(如图所示),位置与图像相同,价格 有一个行动表。

现在我已经为 价格字段 的操作表添加了代码。有人可以帮我实现所需的操作吗。谢谢您的帮助... :)

我正在做这样的事情,但它还没有为我工作。 代码是:

- (void) textFieldDoneTouched
{

    [self.site setValue:textField.text forIndex:selectedRow];

    [UIView animateWithDuration:0.3
                     animations:^{
                         CGRect f = textFieldContainerView.frame;
                         f.origin.y = self.view.frame.size.height;
                         textFieldContainerView.frame = f;
                     }];

    [textField resignFirstResponder];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    self.tableView.userInteractionEnabled = YES;

    [self.view endEditing:TRUE];

    [self validateSiteData];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{

        [self textFieldDoneTouched];
    return YES;
}

#pragma mark -
#pragma mark UITextViewDelegate

- (void) textViewDoneTouched
{




    [self.site setValue:textView.text forIndex:selectedRow];

    [UIView animateWithDuration:0.3
                     animations:^{
                         CGRect f = textViewContainerView.frame;
                         f.origin.y = self.view.frame.size.height;
                         textViewContainerView.frame = f;
                     }];

    [textView resignFirstResponder];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    self.tableView.userInteractionEnabled = YES;

    [self validateSiteData];
}


#pragma mark - UIActionSheetDelegate

- (void) actionSheet:(UIActionSheet *)actionSheet1 clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(buttonIndex == 0) {
        self.site.price = @"Free";
       // NSIndexPath *indexPath = [NSIndexPath indexPathForItem:4 inSection:0];

       // [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:100];
        //[textField resignFirstResponder];
       // [textView becomeFirstResponder];
        }
    else if(buttonIndex == 1) {
        self.site.price = @"Paid ($)";
       // NSIndexPath *indexPath = [NSIndexPath indexPathForItem:4 inSection:0];

      //  [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:100];
       // [textField resignFirstResponder];
         //[textView becomeFirstResponder];
        }


    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    [self validateSiteData];

}

【问题讨论】:

    标签: ios objective-c uitableview uitextfielddelegate


    【解决方案1】:

    那么,您希望 iPhone 的返回键转到下一个字段,就像在计算机键盘上按 Tab 键一样?

    一个小提示:我建议使用默认的灰色“返回”键,而不是屏幕截图中的蓝色“完成”键。 “完成”用于当用户完成填写表格并且键盘应该消失时; "return" 用于转到下一个字段或文本行,就像在 Apple 的联系人应用程序中一样。

    与 Apple 的联系人应用程序一样,我还建议您的 UITextField 与其标签(类别、名称...)位于相同的 UITableViewCells 中。我发现屏幕截图中键盘正上方的文本字段有点混乱。如果你这样做,那么textFieldShouldReturn 的以下实现将使下一个单元格的文本字段成为第一响应者。

    - (BOOL) textFieldShouldReturn:(UITextField*)textField {
        // Determine the row number of the active UITextField in which "return" was just pressed.
        id cellContainingFirstResponder = textField.superview.superview ;
        NSInteger rowOfCellContainingFirstResponder = [self.tableView indexPathForCell:cellContainingFirstResponder].row ;
        // Get a reference to the next cell.
        NSIndexPath* indexPathOfNextCell = [NSIndexPath indexPathForRow:rowOfCellContainingFirstResponder+1 inSection:0] ;
        TableViewCell* nextCell = (TableViewCell*)[self.tableView cellForRowAtIndexPath:indexPathOfNextCell] ;
        if ( nextCell )
            [nextCell.theTextField becomeFirstResponder] ;
        else
            [textField resignFirstResponder] ;
        return YES ;
    }
    

    如果您希望我发布整个项目,请告诉我。

    【讨论】:

    • 非常感谢您的详细回复。如果您提供完整的代码会很有帮助。谢谢...
    • 嗨@John sauer。我通过更改尝试使用您的代码: TableViewCell* nextCell as - UITableViewCell* nextCell 但它不适用于(给出错误)= [nextCell.theTextField becomeFirstResponder] ;所以我尝试将其更改为= [textField becomeFirstResponder] ;但它仍然不适合我......请在这里帮忙。
    • Here's the whole project。如果您对 Xcode 和 Cocoa 足够熟悉,您应该能够弄清楚它是如何工作的,并在您自己的项目中实现类似的东西。
    • thxs john 我将尝试解决该项目并获得我需要的东西.cheers
    • 整个项目链接已过期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多