iXc*_*der 16 iphone objective-c uitableview ios
UITableView提供了方法indexPathsForVisibleRows和visibleCells,但我怎么能得到明显的板块?
Chr*_*ing 20
或者真正简单的方法是利用valueForKeyPath和NSSet类:
NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];
Run Code Online (Sandbox Code Playgroud)
基本上,您在可见行中获取一个节值的数组,然后使用此方法填充一个集以删除重复项.
Swift版本
if let visibleRows = tableView.indexPathsForVisibleRows {
let visibleSections = visibleRows.map({$0.section})
}
Run Code Online (Sandbox Code Playgroud)
UITableViews使用NSIndexPath存储它们的单元格.因此,部分没有对象.使用下面的代码,我们可以遍历表并使用可见部分的索引执行操作(我不确定为什么你想要可见的部分,因为可见仅表示它们当前在屏幕上,但无论如何).
for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows])
{
NSUInteger sectionPath = [i indexAtPosition:0];
//custom code here, will run multiple times per section for each visible row in the group
}
Run Code Online (Sandbox Code Playgroud)
我已经找到解决办法了。
第一步,每个部分将显示由 创建的 UIView - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,该 UIView 将存储到数组中。
当TableView滚动时,我想释放不可见的部分视图,所以我需要知道哪个部分是可见的或不可见的,下面的函数代码将为此目的进行检测,如果视图可见则释放它。
-(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView*)containerView
{
CGPoint point = containerView.contentOffset;
CGFloat zy = point.y ;
CGFloat py = rect.origin.y + rect.size.height;
if (py - zy <0) {
return FALSE;
}
CGRect screenRect = containerView.frame;
CGFloat by = screenRect.size.height + zy ;
if (rect.origin.y > by) {
return FALSE;
}
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
(rect是该部分的框架UIView;containerView是UITableView)
通过这种方式,我可以获得可见的部分UITableView,但我希望SDK可以直接提供用于此目的的API。
| 归档时间: |
|
| 查看次数: |
17831 次 |
| 最近记录: |