在表格视图中,我显示"未找到结果"如果搜索控制器没有通过添加其他条件返回结果 -
在numberOfRowsInSection
:
else if self.searchController.active && self.searchController.searchBar.text?.characters.count > 0 &&
self.filteredLanguages.count == 0 {
return 1
}
在cellForRowAtIndexPath
:
else if self.searchController.active && self.searchController.searchBar.text?.characters.count > 0 &&
self.filteredLanguages.count == 0 {
cell.textLabel?.text = "No results found"
cellImage = nil
cell.userInteractionEnabled = false
isNoResultCell = true
}
if filteredIndexPath!.row == userSettings.valueForKey("selectedRow") as? Int && !isNoResultCell {
cell.tintColor = UIColor.whiteColor()
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
lastSelectedRow = filteredIndexPath
}
我做了一些关于展示方法的研究"没有找到结果"在表视图中但是在写完上面的逻辑之后。
根据几条帖子,我可以在没有搜索结果时在UILabel
的表格的背景视图中添加numberOfSectionsInTableView
作为子视图,如果返回搜索结果则删除子视图。
我的问题是 - 哪个是最具资源/性能的选项?
答案 0 :(得分:3)
试试这个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (youHaveData)
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
yourTableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height)];
noDataLabel.text = @"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
yourTableView.backgroundView = noDataLabel;
yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
答案 1 :(得分:3)
当你的tableview有记录时你需要隐藏一个UILabel
(即数组中有记录将在tableview中显示)。
当您搜索记录并且没有记录时,您可以隐藏tableview并显示文本“找不到结果”。
答案 2 :(得分:0)
我可以肯定地推荐你这个伟大的图书馆,https://github.com/dzenbot/DZNEmptyDataSet它将代表你工作。您可以轻松地将常规字符串或属性字符串或图像设置为占位符,以代替UICollectionView
或UITableView
。
当您将数据重新加载对象时,它将隐藏该占位符。
使用:
在viewDidLoad
:
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
实施以下委托:
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"No results found.";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
有关更多信息和用法,请查看以上链接。
哦,是的,在Swift项目中使用它:How to call Objective-C code from Swift。
答案 3 :(得分:-1)
将UILabel放在桌子后面。当表计数为0时,然后使表'#34; isHidden" =真。这将隐藏表格,然后您将看到它下面的图层。将显示UI标签,因为它直接位于tableview下方。