UITableView check if the index is shown

谁都会走 提交于 2019-12-24 06:49:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!