Nat*_*han 17 iphone uitableview
我有一个UITableView,我正在添加一行动画(使用insertRowsAtIndexPaths:withRowAnimation:
).只要表格不长于屏幕,这一切都很好.
如果它比屏幕大,那么我试图滚动到底部,但它不是我想要的工作.如果我在添加后滚动到新行,我会想念动画.如果我尝试在添加之前滚动到该indexPath,它会抛出异常(关于它不是有效的indexPath)
除了添加空白行之外,还有其他解决方案吗?
小智 22
是的,有一个没有添加空行的解决方案.
注意:在代码中,我认为只有1个部分但很多行.您也可以修改代码以管理多个部分.
- (void)theMethodInWhichYouInsertARowInTheTableView
{
//Add the object in the array which feeds the tableview
NSString *newStringObject = @"New Object";
[arrayWhichFeeds addObject:newStringObject];
[myTableView beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1) inSection:0]];
[myTableView insertRowsAtIndexPaths:paths withRowAnimation:NO];
[myTableView endUpdates];
[myTableView reloadData];
[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
Cas*_*eyB 20
我有同样的问题.这是我的解决方案.
首先 - 更新您的数据源
第二 -
NSIndexPath *path = [NSIndexPath indexPathForRow:([arrayWhichFeeds count] - 1 inSection:0]];
NSArray *paths = [NSArray arrayWithObject:path];
[myTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[myTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Run Code Online (Sandbox Code Playgroud)
*请注意,这不是包裹在[tableView beginUpdades]
和[tableView endUpdates]
代码.如果你这样做将无法按预期工作.
尝试一下,它应该在滚动到它们的同时从底部动画新的行.
其他技术,包括在这个问题下提到的那些技术对我来说不起作用.但这确实如此:
当我第一次开始iOS开发时,我使用了这种技术(并写了这个答案),但这确实是长期的.
归档时间: |
|
查看次数: |
20384 次 |
最近记录: |