Reputation: 4042
Everything works but can't assign value to the UILable of custom cell of searchResultsTableView. I am using the same custom UITableViewCell for my table view controller and it's searchDisplayController. I am using storyboard for the table view controller and table view cell. As you can see in the code, if I NSLog the value of self.searchResults[indexPath.row][@"name"], I can see the result in the console, but if I assign this value to cell.name.text, it won't work, but in the table view controller, it's working. The cell's imageView is working, I just can't assign any value to the cell's UILabel.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchDisplayController.searchResultsTableView registerClass:[LocalCell class] forCellReuseIdentifier:@"Cell2"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
if (tableView == self.searchDisplayController.searchResultsTableView) {
LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
if (!cell) {
cell = [[LocalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
cell.name.text = self.searchResults[indexPath.row][@"name"];
NSLog(@"%@", self.searchResults[indexPath.row][@"name"]); // has value
NSLog(@"%@", cell.name.text); // null
return cell;
} else
{
LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[LocalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.name.text = self.restaurants[indexPath.row][@"name"];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
float radius = 1000.0f;
NSString *searchContent = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
if ([searchContent length] > 0) {
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=%f&types=food&key=%@&sensor=true&name=%@", [[[NSUserDefaults standardUserDefaults] objectForKey:@"lat"] floatValue], [[[NSUserDefaults standardUserDefaults] objectForKey:@"lon"] floatValue], radius, AppKey, searchContent];
__weak LocalViewController *weakSelf = self;
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *taks = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
weakSelf.nextPageToken = dict[@"next_page_token"];
weakSelf.searchResults = dict[@"results"];
NSLog(@"search results: %@", weakSelf.searchResults);
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.searchDisplayController.searchResultsTableView reloadData];
});
}
}];
[taks resume];
}
}
LocalCell.h
#import <UIKit/UIKit.h>
@interface LocalCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (weak, nonatomic) IBOutlet UILabel *distance;
@end
Upvotes: 0
Views: 1376
Reputation: 603
I looked in your project:
NSLog(@"%@", cell.name.text); // null
Because cell.name is nil. The explanation is: then you load your normal tableView, the cell "LocalCell" is loading from Storyboard, allocating and initialising your labels, but then you using self.searchDisplayController.searchResultsTableView you initialise "LoadCell", but never initialise his labels (because they are weak IBOutlets - and used in storyboard).
To solve your problem, I found 3 solutions: 1. Create another SearchCell with strong references to his UILabels, initialise all UILabel s in -(id)init of SearchCell. Then use it in your controller.
[self.searchDisplayController.searchResultsTableView registerClass:[SearchCell class] forCellReuseIdentifier:@"SearchCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *SearchCellIdentifier = @"SearchCell";
Restaurant *res;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchCellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
res = (Restaurant *)self.searchResults[indexPath.row];
cell.nameLabel.text = res.name;
cell.descLabel.text = res.desc;
// image load
return cell;
}
else
{
LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[LocalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
res = (Restaurant *)self.restaurants[indexPath.row];
cell.nameLabel.text = res.name;
cell.descLabel.text = res.desc;
// image load
return cell;
}
}
Another way is to create a nib with your cell "SearchCellNib", and use your created LoadCell class. For load nib use:
cell = (LoadCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (LoadCell*)[[[NSBundle mainBundle] loadNibNamed:@"SearchCellNib" owner:nil options:nil] objectAtIndex:0];
}
Or use UITableViewCell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchCellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
res = (Restaurant *)self.searchResults[indexPath.row];
cell.textLabel.text = res.name;
cell.detailTextLabel.text = res.desc;
Upvotes: 2