DidselectRowAtIndexPath未按预期/期望返回indexPath.row

时间:2015-02-19 15:50:57

标签: ios objective-c uitableview

  • 我有一个11行的视图,其中包含textfields,textViews
  • 所有元素的总高度>的 ScreenHeight
  • 我有一个textfield作为第二个元素。
  • 将空行作为最后一个元素

为了呈现此视图,我使用了TableView

  • 我对不同类型的元素使用不同的标识符。
  • CellForRowAtIndexPath中,我根据indexaPath.row
  • 返回了单元格
  • 视图按预期显示。
  • 我滚动,静止行按预期显示

由于Textfield在最后并且呈现了键盘,所以

  • 单击textField时
  • 我将tableView.frame向上移动keyboard.height
  • 正确向上移动。
  • 我看到cellForIndexPath调用了最后一个元素(indexPath.row = 10)

现在问题开始

  • textField.cell.row = 9(第10个元素)
  • 要关闭键盘,我按下“textFieldCell”(最后一个元素)下面的单元格,该单元格预计是indexPath.row = 10
  • 的行
  • 在textField.didEndEditing中,我重置了tableView.frame = originalFrame
  • scrollToBottomAnimated

  • 现在我看到,iOS调用cellForIndexPath = 7,6,5,4

  • 但是 didSelectRowAtIndexPath 给了我rowNumber = 7 which is not what I clicked

  • 我点击了第11行或indexPath.row = 10

但是由于调整了tableView和scolling的大小,出了问题。

我做错了什么。

当我点击最后一个单元格时,我需要做什么才能获得indexPath.row = 10而不是7?

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"moveView" context:nil];
    [UIView setAnimationDuration:0.0];

    self.frame=CGRectMake(0,66,ScreenWidth,ScreenHeight - keyboardFrame.size.height-66);
    [UIView commitAnimations];

    [self scrollToBottomRowAnimated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView beginAnimations:@"moveView" context:nil];
    [UIView setAnimationDuration:0.0];
    self.frame=CGRectMake(0,66,ScreenWidth,ScreenHeight - 66);
    [UIView commitAnimations];
    [self scrollToBottomRowAnimated:YES];
}
- (void)scrollToBottomRowAnimated:(BOOL)animated
{
    long lastRowNumber = [self numberOfRowsInSection:0]-1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
    [self scrollToRowAtIndexPath:indexPath
                          atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(indexPath.row);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

}

1 个答案:

答案 0 :(得分:2)

我怀疑(不确定)问题是你真的混合了两种类型的动画,并且在缩小表格视图以补偿键盘的方法中。

不要通过已提交的动画使用tableview框架更改。相反,我建议在表格视图中使用一个简单的垂直内容偏移。 UITableView实际上是一个UIScrollView子类,因此它具有scrollview的所有方法,并且支持其内容的垂直内容偏移。

在textfieldDidBeginEditing中:

[self.tableView setContentOffset:CGPointMake(0, kKeyboardHeight) animated:YES];

并在textfieldDidEndEditing中通过

删除偏移量
[self.tableView setContentOffset:CGPointZero animated:YES];
相关问题