2014-09-05 45 views
0

我试图让我的应用程序中的视图列表上的SQL数据,好吧,一切都好,启动应用程序,但是当我看到viewlist(没有sql数据),我看到这个错误:Xcode dataSource必须从tableView中返回一个单元格:cellForRowAtIndexPath

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 
+3

通话到'dequeueReusableCellWithIdentifier:'可以返回'nil'。如果是这样,你需要创建单元格。 – rmaddy 2014-09-05 15:04:14

+0

好的,谢谢我解决了,是第二次重做一切,我忘了指定单元格的标识符:) – 2014-09-05 15:12:11

回答

1

只是把这个代码行的cellForRowAtIndexPath方法

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