问题
in order to get the necessary height of a cell, I implemented a function like
+(CGFloat)rowHeightForTableView:(UITableView*)tableView andObject:(NSObject*)theObject {...}
within the cells.
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?
回答1:
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");
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).
来源:https://stackoverflow.com/questions/4287621/uitableview-check-if-the-index-is-shown