epo*_*gee 9 cocoa-touch scroll objective-c uitableview
我正在尝试用日期创建一个UITableView,不应该是非常令人兴奋的,我知道.它从当前日期开始,但用户应该能够向下(未来)向上(向上)向他/她想要.这导致可能无限数量的行.那么创建它的方法是什么?
返回NSIntegerMax的行数已经崩溃的应用程序,但即使它不会,这仍然没有占到能够滚动起来.我当然可以从中途开始,但最终会有一个最大值.
任何想法怎么做或假冒这个?我可以以某种方式更新/重新加载表,而无需用户注意,所以我从来没有碰到边框?
解决方案:
我选择了@ ender的建议并制作了一个包含固定数量单元格的表格.但是当用户滚动到固定单元格的边缘附近时,我没有重新加载它,而是在滚动停止时重新加载表格.为了适应用户不停地滚动很远的距离,我只是将行数增加到1000,将ROW_CENTER常量设置为500.这是负责更新行的方法.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSArray *visible = [self.tableView indexPathsForVisibleRows];
NSIndexPath *upper = [visible objectAtIndex:0];
NSIndexPath *lower = [visible lastObject];
// adjust the table to compensate for the up- or downward scrolling
NSInteger upperDiff = ROW_CENTER - upper.row;
NSInteger lowerDiff = lower.row - ROW_CENTER;
// the greater difference marks the direction we need to compensate
NSInteger magnitude = (lowerDiff > upperDiff) ? lowerDiff : -upperDiff;
self.offset += magnitude;
CGFloat height = [self tableView:self.tableView heightForRowAtIndexPath:lower];
CGPoint current = self.tableView.contentOffset;
current.y -= magnitude * height;
[self.tableView setContentOffset:current animated:NO];
NSIndexPath *selection = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
if (selection)
{
// reselect a prior selected cell after the reload.
selection = [NSIndexPath indexPathForRow:selection.row - magnitude inSection:selection.section];
[self.tableView selectRowAtIndexPath:selection animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
Run Code Online (Sandbox Code Playgroud)
当用户在不停止的情况下滚动到桌面边缘时,魔术会中断,但是在bounces禁用表视图属性的情况下,这仅仅是一个小故障,但完全可以接受.一如既往,感谢StackOverflow!
当用户滚动到tableview的末尾附近时,您应该建立固定数量的单元格并调整数据源.例如,您有一个包含51个日期的数组(今天,25个未来和25个过去).当应用尝试在其中一个边框附近渲染单元格时,请重新配置阵列并调用reloadData
| 归档时间: |
|
| 查看次数: |
2866 次 |
| 最近记录: |