【问题标题】:how to hide section title if result not found using searchbar in ios8如果在 ios 8 中使用搜索栏找不到结果,如何隐藏部分标题
【发布时间】:2015-10-14 08:15:56
【问题描述】:

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_segment.selectedSegmentIndex==0)
    {
        return sortedKeys;
    }
    else
    {
       return sortedKeys1;
    }
}

我使用此代码,但如果名称不存在,我不想要部分标题,现在它给我所有部分标题

【问题讨论】:

  • 你是否尝试返回 0 个部分?
  • 是的,我在 numberrowsinsection 中返回 0
  • 我的意思是 numberofsection,而不是 numberrowsinsection。
  • nop 我返回他们的数组
  • 你能在 numberofsection 中发布你返回的行吗?

标签: ios objective-c iphone searchbar


【解决方案1】:

如果没有行,则将节的标题设置为零。

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0)
 {
                return nil;
    } else {
        return "section title \(section)"
    }
   return @"";
}

这会起作用。

【讨论】:

  • 欢迎..快乐编码
【解决方案2】:

尝试在numberOfSectionsInTableView中返回0

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

     if (result_not_found) { /// pass the condition when "result not found" here
        return 0;
     }

     if (_segment.selectedSegmentIndex==0) { 
        return ([sortedKeys count]); 
     } 
     else { 
        return ([sortedKeys1 count]); 
     } 
}

【讨论】:

  • 试试这个,如果一样的话。也许@vijay 的答案是正确的。
  • 是的 anhtu ,vijay 是正确的,它的工作非常感谢您的帮助
  • 如果 2 个答案正确,您应该接受第一个给出者的答案
  • @NitinGohel,我在 vijay 之前给出了答案。但是vijay的答案是正确的。谢谢
  • 两者都是正确的想要标记两者但只选择了一个anhtu
【解决方案3】:

您应该使用这个返回正确数量的部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 

如果相应部分没有行,则返回 CGFLOAT_MIN :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

希望对你有帮助。

汤姆

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2021-11-06
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多