【发布时间】:2013-12-24 17:33:59
【问题描述】:
一切正常,但无法为 searchResultsTableView 的自定义单元格的 UILable 赋值。我为我的表格视图控制器使用相同的自定义 UITableViewCell,它是 searchDisplayController。我正在为表视图控制器和表视图单元使用情节提要。正如您在代码中看到的,如果我对 self.searchResults[indexPath.row][@"name"] 的值进行 NSLog 记录,我可以在控制台中看到结果,但是如果我将此值分配给 cell.name.text ,它不起作用,但在表格视图控制器中,它正在工作。单元格的 imageView 正在工作,我只是无法为单元格的 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
【问题讨论】:
-
显示LocalCell的代码
-
cell.textLabel.text = @"一些字符串";因为您的 LoadCell 具有 UITableViewCellStyleDefault 样式
-
首先,如果您使用了两个单独的标识符进行出队和分配,您也可以确保您尝试访问的标签不为零。
-
@prasad 我的错,我只是将它们更改为相同的单元格标识符,仍然无法解决问题。
-
@prasad 我在 cellForRowAtIndexPath 中使用 [cell.name isKindOfClass:[NSNull class]] 来确定标签是否为空,但它告诉我它不是空的。
标签: ios iphone uitableview