UITableView检查索引是否显示 [英] UITableView check if the index is shown
问题描述
为了获得必要的单元格高度,我实现了类似的功能
in order to get the necessary height of a cell, I implemented a function like
+(CGFloat)rowHeightForTableView:(UITableView*)tableView andObject:(NSObject*)theObject {...}
在单元格内.
由于tableView是参数之一,因此我可以检查它是普通样式表还是分组样式表,并将结果宽度包括在我的计算中.但是我应该如何检查是否显示索引?
Since the tableView is one of the parameters, I can check whether it's a plain or grouped style table and include the resulting width in my calculations. But how should I check if an index is being shown?
推荐答案
您可以检查表视图的数据源是否响应sectionIndexTitlesForTableView:
选择器(数据源必须为索引表视图实现该选择器):
You can check if the table view's datasource responds to the sectionIndexTitlesForTableView:
selector (which a datasource must implement for an indexed table view):
if ([tableView.dataSource
respondsToSelector:@selector(sectionIndexTitlesForTableView:)])
{
NSArray *result =
[tableView.dataSource sectionIndexTitlesForTableView:tableView];
if (result != nil)
NSLog(@"tableView is currently indexed");
else
NSLog(@"tableView is not currently indexed");
}
else
NSLog(@"tableView does not implement indexing");
如果数据源响应该选择器,则实际上对其进行调用并检查结果,就可能导致表视图当前正在从该方法返回nil,因为由于某种原因或由于数据源而不希望显示索引,正在处理多个表视图(其中一些被索引而另一些则没有).
If the datasource responds to that selector, actually calling it and checking the result allows for the possibility that the table view is currently returning nil from that method because it doesn't want to show the index for whatever reason or if the datasource is handling multiple table views (some of which are indexed and some which aren't).
这篇关于UITableView检查索引是否显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!