【问题标题】:Pressing the magnifying glass icon on a UiTableView Index按下 UiTableView 索引上的放大镜图标
【发布时间】:2012-04-08 00:09:42
【问题描述】:

我正在使用以下代码来显示 TableView 的索引。 我的问题是按下放大镜图标并没有将我带到搜索栏所在的表格顶部。索引的所有其他部分都有效。

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

if (tableView == [[self searchDisplayController] searchResultsTableView])
    return nil;
else {
    //I add the magnifying glass to the index
    NSArray *indexArray = [NSArray arrayWithObject:UITableViewIndexSearch];
    // I return the array for the index after I add the rest of Index items
    return [indexArray arrayByAddingObjectsFromArray:self.sectionsarray];
}


}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
// I set index - 1 so I can compensate for the addition of the magnifying glass
return index -1;
} 

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    -1 部分是什么,我不知道,您可以使用以下代码代替表头部分:

    if(index == 0)     
      [tableView scrollRectToVisible:CGRectMake(0, 0, self.mySearchBar.width, self.mySearchBar.height) animated:YES];
    return index -1;
    

    【讨论】:

    • 由于您没有-1 索引部分,因此选择索引为0(玻璃图标)的标题并不能满足您的要求。相反,您必须手动处理这种情况并将滚动视图内容滚动到具有搜索栏的左上角。那是sectionForSectionIndexTitle方法的内容。
    • 不错的解决方案。不过,我选择使用动画:NO,因为这给出了类似联系人应用程序的结果。此外,由于我在搜索栏上没有 IBOutlet,所以只要 x=y=0,任意大小的矩形似乎也可以。
    • 此代码适用于 iOS 6:if(index==0) return NSNotFound; :o)
    • @Cédric Belin,感谢您的提示,您有没有在文档或任何 Apple 源代码中看到它?
    • Apple 的文档没有指定如果未找到该部分要返回什么值。作为NSArray:indexOfObject: 的方法,我觉得NSNotFound return 比-1 更合适。但是我的回答太快了(我应该删除我的评论):返回 NSNotFound 与返回 -1 的效果相同,视图返回到表格顶部而不是搜索栏顶部。我被误导了,因为当我进行测试时,我正在处理一个简短的列表(5-6 项)。你的答案绝对是正确的(直到 iOS 7?)。
    猜你喜欢
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多