UITableViewController中的断言失败 [英] Assertion Failure in UITableViewController

查看:117
本文介绍了UITableViewController中的断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在参加iTunes U Stanford iOS Class,正在开展其中一项任务,以构建一个小小的Flickr应用程序。我收到一个错误,我似乎无法调试,因为

I am taking the iTunes U Stanford iOS Class and am working on one of the assignments to build a little Flickr app. I am getting an error that I cannot seem to be able to debug which comes up as


* 断言失败in - [UITableView _configureCellForDisplay:forIndexPath:],/ SourceCache / UIKit_Sim / UIKit-2280.1 / UITableView.m:5336 2012-08-03
10:59:24.596作业4 [4611:c07](null)libc + + abi.dylib:终止
,称为抛出异常

* Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2280.1/UITableView.m:5336 2012-08-03 10:59:24.596 Assignment 4[4611:c07] (null) libc++abi.dylib: terminate called throwing an exception

我对相关tableviewcontroller的代码:

My code for the tableviewcontroller in question:

#import "PlacesPhotosTableViewController.h"

@interface PlacesPhotosTableViewController ()
@property (nonatomic) NSDictionary *placeToDisplay;
@property (nonatomic) NSArray *photosInPlace;
@end

@implementation PlacesPhotosTableViewController
@synthesize placeToDisplay = _placeToDisplay;
@synthesize photosInPlace = _photosInPlace;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    self = [super init];

    return self;
}

- (id)initWithPlace:(NSDictionary *)place
{
    self = [self init];
    if (self)
    {
        self.placeToDisplay = place;
    }

    return self;
}

- (NSArray *)photosInPlace
{
    if (_photosInPlace == nil)
    {
        _photosInPlace = [FlickrFetcher photosInPlace:self.placeToDisplay maxResults:50];
    }

    return _photosInPlace;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog([self.placeToDisplay valueForKeyPath:@"description._content"]);

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.photosInPlace count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Photos";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSString *string = [[self.photosInPlace objectAtIndex:indexPath.row] valueForKeyPath:@"description._content"];

    cell.textLabel.text = string;

    return cell;
}


推荐答案

CellIdentifier 我打赌你的 cellForRowAtIndexPath 正在返回 nil

CellIdentifier I bet your cellForRowAtIndexPath is returning nil.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"Photos";

    /** NOTE: This method can return nil so you need to account for that in code */
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // NOTE: Add some code like this to create a new cell if there are none to reuse
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    NSString *string = [[self.photosInPlace objectAtIndex:indexPath.row]     valueForKeyPath:@"description._content"];

    cell.textLabel.text = string;

    return cell;
}

这可能是为什么 [UITableView _configureCellForDisplay:forIndexPath:] 失败...因为 cellForRowAtIndexPath 返回空值然后 configureCellForDisplay 正在等待a UITableViewCell

That's probably why [UITableView _configureCellForDisplay:forIndexPath:] is failing... because cellForRowAtIndexPath is returning a null value and then configureCellForDisplay is expecting a UITableViewCell.

这篇关于UITableViewController中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆