当用户到达 UITableView 的最后一行时,如何动态添加行?
我有一个 UITableview,当前显示 10 行,该行是固定静态的。现在我想在其中添加一个功能。当用户到达 UITableView
的最后一行时,我想向表中添加更多 10 行。我的意思是目前我在应用程序中显示固定的 10 行。
但现在我想在用户到达上一个 UITableview
的最后一行时再添加 10 行。
请给我任何建议或任何示例代码来实现我的动机。提前致谢。
I have a UITableview
which shows 10 rows currently which is fixed static. Now I want to add a feature into it. I want to add a more 10 rows to the table when user reach to the last row of the UITableView
. I mean currently I am showing fixed 10 rows in the application.
But now I want to add 10 more rows whenever user reaches to the last row of previous UITableview
.
Please give me any suggestions or any sample code to achieve my motive. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其实很简单。您需要做的是实现
tableView:willDisplayCell:forRowAtIndexPath:
方法,该方法属于UITableViewDelegate
协议。每次要显示单元格时都会调用此方法。因此,它会让您知道最后一个单元格即将显示的时间。然后你可以做类似的事情 -另一个(有点数学)选项是实现
UIScrollView
委托方法(UITableView
是UIScrollView
的子类),即scrollViewDidEndScrollingAnimation:
或scrollViewDidScroll:
。这些将使您知道用户正在查看的内容的 y 位置。如果发现最底部的内容可见,您可以添加更多项目。It is actually quite simple. What you need to do is implement the
tableView:willDisplayCell:forRowAtIndexPath:
method, which belongs to theUITableViewDelegate
protocol. This method hits every time a cell is about to be displayed. So, it will let you know when the last cell is about to be displayed. Then you could do something like-Another (a bit mathematical) option is to implement
UIScrollView
delegate methods (UITableView
is a subclass ofUIScrollView
), namelyscrollViewDidEndScrollingAnimation:
orscrollViewDidScroll:
. These will let you know the y-position of the content the user is viewing. If it is found that the bottom most content is visible, you can add more items.uitableview
派生自uiscrollview
。为了实现您的目标,您需要实现scrollViewDidEndDecelerating
:这将检测一种“弹跳效果”,例如向上移动可见行以表明人们希望看到更多。
uitableview
is derived fromuiscrollview
. To achieve your objective, you need to implementscrollViewDidEndDecelerating
:This will detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.
您到底想如何调用额外 10 行的加载?当用户只是向下滚动查看默认加载的前 10 个时,他可能不想再加载 10 个。
您可以添加“添加更多”行作为表格的最后一行。当用户点击这个时,您会再添加 10 个。
(我不知道如何检测“弹跳效果”,例如向上移动可见行以表明想要查看更多内容。)
基本逻辑如下所示:
cellForRowAtIndexPath
中,您检查用户是否单击了最后一行,然后调用您的代码来添加 10 行[myTable reloadData]
numberOfRowsInSection
增加 10 并确保cellForRowAtIndexPath
能够正确返回新行 11-20ps(如果您确实希望在用户到达末尾时加载 10 行)表,您需要在
cellForRowAtIndexPath
或willDisplayCell:forRowAtIndexPath:
中调用加载 10 个以上的数据叫你最后一行。How would you exactly want to invoke the loading of the additional 10 rows? When a user just scrolls down to see the first 10 that are loaded by default he might not want to load 10 more already.
You may add a "add more" line as the last row of your table. When the user clicks on this one you add 10 more.
(I don't know how one would detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.)
The basic logic would look like this:
cellForRowAtIndexPath
you check if the user clicked on the last line and then invoke your code to add the 10[myTable reloadData]
numberOfRowsInSection
by 10 and make sure thatcellForRowAtIndexPath
will correctly return your new lines 11-20ps if you REALLY want 10 additional rows to be loaded when the user reaches the end of the table you need to invoke the loading of 10 more in
cellForRowAtIndexPath
orwillDisplayCell:forRowAtIndexPath:
when it is called for your last line.这里的categoriesList是一个数组。我们可以将对象添加到该数组中,并在 tableview 中调用 reloadData。
Here categoriesList is an array. We can add objects to this array and call reloadData in the tableview.