ios - Obj-c - 如果没有搜索结果,返回表格视图中的 1 个单元格?
问题描述
当我的用户在 tableview 中搜索某些内容时,当没有返回任何结果时,我的应用程序会在 tableview 中显示标准的“无结果”占位符。也就是说,当不存在结果时,我想返回一个填充的单元格(填充有默认数据的单元格)。我怎样才能做到这一点?我尝试了以下方法,但仍然返回“无结果”?
视图控制器.m
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchResults count] == 0) {
return 1;
} else {
return [searchResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NetworkTableIdentifier = @"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = nib[0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSDictionary *userName = searchResults[indexPath.row];
NSString *first = userName[@"first name"];
NSString *last = userName[@"last name"];
[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first, last]];
NSDictionary *userlast = searchResults[indexPath.row];
[[cell lastName] setText:userlast[@"last name"]];
NSDictionary *userBio = searchResults[indexPath.row];
[[cell userDescription] setText:userBio[@"userbio"]];
NSString *area = userName[@"neighbourhood"];
NSString *city = userName[@"city"];
[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", area, city]];
NSString *profilePath = searchResults[indexPath.row][@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
if ([searchResults count] == 0) {
NSLog(@"SEARCH RESULTS ARE %@", searchResults);
[[cell username] setText:[NSString stringWithFormat:@"%@", self.searchBar.text]];
[[cell lastName] setText:userlast[@""]];
[[cell userDescription] setText:@"This friend is not on the app (yet!) Tap to invite them."];
[[cell areaLabel] setText:[NSString stringWithFormat:@""]];
NSString *profileDefault = @"http://url.com/user.png";
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profileDefault]];
return cell;
}
return cell;
}
解决方案
试试这个它的工作:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (arrData.count>0)
{
self.TblView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.TblView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.TblView.bounds.size.width, self.TblView.bounds.size.height)];
noDataLabel.text = @"No Results";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
self.TblView.backgroundView = noDataLabel;
self.TblView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
推荐阅读
- python - 如何将 lambda 应用于每个数据帧行,以便测试两个条件?
- swift - 在 SwiftUI 中,如何将组件固定到屏幕底部?
- android - 透明的白色按钮在 Android Material Design 中看起来很糟糕
- ios - CoreData XCTest 测试在一起运行时失败,但一次没有一个
- javascript - 如何在模态下打开 Youtube 视频
- javascript - 从 c# 调用 javascript 函数以使用 esri 请求调用地图服务
- android - 为什么我收到paypal系统错误请重试
- java - 当用户输入 1 - 2 时,scanner.nextLine() 会导致错误。如何让我的程序接受此输入为 1?
- java - 即使 authenticationrequired 为 false,创建 MasterKey 也会导致 UserNotAuthenticated
- javascript - 为什么右键单击事件比左键慢?