【问题标题】:Can't assign to the UILabel of custom cell of searchResultsTableView无法分配给 searchResultsTableView 的自定义单元格的 UILabel
【发布时间】: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


【解决方案1】:

我查看了您的项目:

NSLog(@"%@", cell.name.text); // null 

因为 cell.name 为 nil。 解释是:然后你加载你的普通 tableView,单元格“LocalCell”从 Storyboard 加载,分配和初始化你的标签,但是你使用 self.searchDisplayController.searchResultsTableView 你初始化“LoadCell”,但从不初始化他的标签(因为他们是弱 IBOutlets - 并用于情节提要)。

为了解决您的问题,我找到了 3 个解决方案: 1.创建另一个SearchCell,强引用他的UILabels,初始化SearchCell的-(id)init中的所有UILabel。然后在你的控制器中使用它。

[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;
}
}

另一种方法是使用您的单元格“SearchCellNib”创建一个笔尖,并使用您创建的 LoadCell 类。加载笔尖使用:

cell = (LoadCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = (LoadCell*)[[[NSBundle mainBundle] loadNibNamed:@"SearchCellNib" owner:nil options:nil] objectAtIndex:0];
    }

或者使用 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多