Xcode dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格

时间:2014-09-05 14:59:37

标签: ios objective-c uitableview

我试图在我的应用程序中的视图列表上获取sql数据,确定它没关系,启动应用程序但是当我看到视图列表(没有sql数据)时,我看到了这个错误:< / p>

2014-09-05 16:33:21.54 Today[28849:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010194f495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016ae99e objc_exception_throw + 43
    2   CoreFoundation                      0x000000010194f31a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x000000010124af19 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
    4   UIKit                               0x0000000100329a60 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 346
    5   UIKit                               0x00000001002b3c2c +[UIView(Animation) performWithoutAnimation:] + 70
    6   UIKit                               0x0000000100329900 -[UITableView _configureCellForDisplay:forIndexPath:] + 101
    7   UIKit                               0x000000010032ffa3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 373
    8   UIKit                               0x0000000100315d5b -[UITableView _updateVisibleCellsNow:] + 2337
    9   UIKit                               0x0000000100327721 -[UITableView layoutSubviews] + 207
    10  UIKit                               0x00000001002bb993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    11  QuartzCore                          0x0000000103f39802 -[CALayer layoutSublayers] + 151
    12  QuartzCore                          0x0000000103f2e369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    13  QuartzCore                          0x0000000103f2e1ea _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    14  QuartzCore                          0x0000000103ea1fb8 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
    15  QuartzCore                          0x0000000103ea3030 _ZN2CA11Transaction6commitEv + 394
    16  QuartzCore                          0x0000000103ea369d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    17  CoreFoundation                      0x000000010191adc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    18  CoreFoundation                      0x000000010191ad37 __CFRunLoopDoObservers + 391
    19  CoreFoundation                      0x00000001018fa522 __CFRunLoopRun + 946
    20  CoreFoundation                      0x00000001018f9d83 CFRunLoopRunSpecific + 467
    21  GraphicsServices                    0x0000000103ac6f04 GSEventRunModal + 161
    22  UIKit                               0x000000010025be33 UIApplicationMain + 1010
    23  Today                       0x0000000100003043 main + 115
    24  libdyld.dylib                       0x0000000101fe75fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我不明白错误的位置,因为Xcode没有显示任何错误 这些是有问题的文件我希望有人可以帮助我,希望这是一些废话..

(适用MyWeekController.m)

#import "MyWeekViewController.h"
#import "Location.h"

@interface MyWeekViewController ()
{
    HomeModel *_homeModel;
    NSArray *_feedItems;
}
@end

@implementation MyWeekViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set this view controller object as the delegate and data source for the table view
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;

    // Create array object and assign it to _feedItems variable
    _feedItems = [[NSArray alloc] init];

    // Create new HomeModel object and assign it to _homeModel variable
    _homeModel = [[HomeModel alloc] init];

    // Set this view controller object as the delegate for the home model object
    _homeModel.delegate = self;

    // Call the download items method of the home model object
    [_homeModel downloadItems];
}

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

-(void)itemsDownloaded:(NSArray *)items
{
    // This delegate method will get called when the items are finished downloading

    // Set the downloaded items to the array
    _feedItems = items;

    // Reload the table view
    [self.listTableView reloadData];
}

#pragma mark Table View Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of feed items (initially 0)
    return _feedItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve cell
    NSString *cellIdentifier = @"BasicCell";
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];



    return myCell;
}

@end

(适用MyWeekController.h)

#import <UIKit/UIKit.h>
#import "HomeModel.h"

@interface MyWeekViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, HomeModelProtocol>

@property (weak, nonatomic) IBOutlet UITableView *listTableView;

@end

(适用HomeModel.h)

#import <Foundation/Foundation.h>

@protocol HomeModelProtocol <NSObject>

- (void)itemsDownloaded:(NSArray *)items;

@end

@interface HomeModel : NSObject <NSURLConnectionDataDelegate>

@property (nonatomic, weak) id<HomeModelProtocol> delegate;

- (void)downloadItems;

@end

(适用HomeModel.m)

#import "HomeModel.h"
#import "Location.h"

@interface HomeModel()
{
    NSMutableData *_downloadedData;
}
@end

@implementation HomeModel

- (void)downloadItems
{
    // Download the json file
    NSURL *jsonFileUrl = [NSURL URLWithString:@"http://mywebsite.netservice.php"];

//在上面的行中我更改了域名以保护隐私,实际上,该链接显示了一个sql结果数组

    // Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

    // Create the NSURLConnection
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Initialize the data object
    _downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the newly downloaded data
    [_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Create an array to store the locations
    NSMutableArray *_locations = [[NSMutableArray alloc] init];

    // Parse the JSON that came in
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

    // Loop through Json objects, create question objects and add them to our questions array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonElement = jsonArray[i];

        // Create a new location object and set its props to JsonElement properties
        Location *newLocation = [[Location alloc] init];
        newLocation.name = jsonElement[@"Name"];

        // Add this question to the locations array
        [_locations addObject:newLocation];
    }

    // Ready to notify delegate that data is ready and pass back items
    if (self.delegate)
    {
        [self.delegate itemsDownloaded:_locations];
    }
}

@end

1 个答案:

答案 0 :(得分:1)

将这行代码放在cellForRowAtIndexPath方法

if (!myCell) {
    myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
}