gpt4 book ai didi

ios - Obj-c - 如果没有搜索结果,则在表格 View 中返回 1 个单元格?

转载 作者:行者123 更新时间:2023-11-29 05:46:47 25 4
gpt4 key购买 nike

当我的用户在表格 View 内搜索某些内容时,如果没有返回结果,我的应用程序会在表格 View 内显示标准的“无结果”占位符。也就是说,当不存在结果时,我想返回一个填充的单元格(填充有默认数据的单元格)。我怎样才能做到这一点?我尝试了以下操作,但仍然返回“无结果”?

ViewController.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;

}

关于ios - Obj-c - 如果没有搜索结果,则在表格 View 中返回 1 个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56081257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com