- 我有一个包含文本字段和 TextView 的 11 行 View
- 所有元素的总高度> ScreenHeight
- 我有一个文本字段作为倒数第二个元素。
- 空行作为最后一个元素
为了呈现这个 View ,我使用了 TableView
- 我对不同类型的元素使用不同的标识符。
- 在
CellForRowAtIndexPath
中,我返回基于indexaPath.row
的单元格> - View 按预期显示。
- 我滚动并且仍然按预期显示行
由于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这不是我点击的内容
。- 我点击了第 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];
}
最佳答案
我怀疑(虽然不确定)问题是您实际上混合了两种类型的动画,并且在缩小表格 View 以补偿键盘的方法中。
不要通过提交的动画使用 tableview 框架更改。相反,我建议在表格 View 中使用一个普通的垂直内容偏移量。 UITableView 实际上是 UIScrollView 的子类,因此它具有 ScrollView 的所有方法,并且支持其内容的垂直内容偏移。
在 textfieldDidBeginEditing 中:
[self.tableView setContentOffset:CGPointMake(0, kKeyboardHeight) animated:YES];
并在 textfieldDidEndEditing 中通过
摆脱偏移量[self.tableView setContentOffset:CGPointZero animated:YES];
关于ios - DidselectRowAtIndexPath 没有按预期/期望返回 indexPath.row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28610740/