[英]Obj-c - If no search results, return 1 cell in tableview?
當我的用戶在 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;
}
我真的不建議這樣做,因為如果沒有搜索結果,您應該返回一個空列表。 這與用戶界面指南一致。 但是,如果您堅持,您可以創建一個默認對象並使用該對象初始化您的 searchResults 數組,並從 numberOfRows 方法返回 1。 像這樣的東西:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchResults count] == 0) {
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@“Enter First Name”, @“Enter Last Name”, @“Enter User Bio”, @“Enter Neighborhood”, @“Enter City”, @“Enter Photo Path”]
forKeys: @[@“first_name”, @“last_name, @“userbio”, @“neighbourhood”, @“city”, @“photo_path”];
searchResults = [NSArray arrayWithObjects: dict, nil];
return 1;
}
else {
return [searchResults count];
}
}
而且,您可以大大簡化 cellForRowAtIndexPath 代碼,如下所示:
- (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 objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
//Note, I like to name my local variables the same as the dictionary keys just to eliminate any confusion
NSDictionary *userObject = [searchResults objectAtIndex:indexPath.row];
NSString *first_name = [userObject objectForKey:@"first name"];
NSString *last_name = [userObject objectForKey:@"last name"];
NSString *userbio = [userObject objectForKey:@“userbio”];
NSString *neighbourhood = [userObject objectForKey:@“neighbourhood”];
NSString *city = [userObject objectForKey:@“city”];
NSString *photo_path = [userObject objectForKey:@“photo_path”];
[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first_name, last_name]];
[[cell lastName] setText:last_name];
[[cell userDescription] setText:userbio];
[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", neighbourhood, city]];
[[cell usermini] sd_setImageWithURL:[NSURL URLWithString:photo_path]];
}
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.