我有一个 UITableview
,当前显示 10 行,该行是固定静态的。现在我想在其中添加一个功能。当用户到达 UITableView
的最后一行时,我想向表中添加更多 10 行。我的意思是目前我在应用程序中显示固定的 10 行。
但现在我想在用户到达上一个 UITableview
的最后一行时再添加 10 行。
请给我任何建议或任何示例代码来实现我的动机。提前致谢。
最佳答案
其实很简单。您需要做的是实现 tableView:willDisplayCell:forRowAtIndexPath:
方法,该方法属于 UITableViewDelegate
协议(protocol)。每次要显示单元格时都会调用此方法。因此,它会让您知道最后一个单元格即将显示的时间。然后你可以做类似的事情-
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [self.array count] - 1) //self.array is the array of items you are displaying
{
//If it is the last cell, Add items to your array here & update the table view
}
}
另一个(有点数学的)选项是实现 UIScrollView
委托(delegate)方法(UITableView
是 UIScrollView
的子类),即 scrollViewDidEndScrollingAnimation:
或scrollViewDidScroll:
。这些将使您知道用户正在查看的内容的 y 位置。如果发现最底部的内容可见,您可以添加更多项目。
HTH,
阿克谢
关于iphone - 当用户到达 UITableView 的最后一行时,如何动态添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7170821/