我正在尝试使用TableView填充KGModal视图(https://github.com/kgn/KGModal),但是在没有应用程序崩溃的情况下通过JSON获取数据后我无法重新加载数据。
cellForRowAtIndexPath
方法正确返回JSON结果,我认为问题出在reloadData方法中,因为_tv已经被KGModal使用了? itemNutriFeed.*
只是一个JSON模型。
// itemNutriModal.h
#import <UIKit/UIKit.h>
@interface itemNutriModal : UIViewController <UITableViewDataSource, UITableViewDelegate>
- (void)showNutri; //:(NSArray*)nutri;
@end
// itemNutriModal.m
#import "itemNutriModal.h"
#import "JSONModelLib.h"
#import "itemNutriFeed.h"
#import "KGModal.h"
@interface itemNutriModal () {
itemNutriFeed* _feed;
UITableView* _tv;
}
@end
@implementation itemNutriModal
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showNutri //:(NSArray*)nutri
{
_tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
_tv.backgroundColor = [UIColor clearColor];
_tv.scrollEnabled = FALSE;
[_tv setDataSource:self];
[_tv setDelegate:self];
[[KGModal sharedInstance] showWithContentView:_tv andAnimated:YES];
_feed = [[itemNutriFeed alloc] initFromURLWithString:@"http://c..../.../nutri.php?inid=1000"
completion:^(JSONModel *model, JSONModelError *err) {
//hide the loader view
//[HUD hideUIBlockingIndicator];
//json fetched
NSLog(@"Results: %@", _feed.results);
[_tv reloadData];
}];
//;
//NSLog(@"SHOW NUTRIS!");
}
#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 8;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"nutriTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
itemNutriModel* nutrivals = _feed.results[indexPath.row];
NSLog(@"nutrivals: %@", _feed.results);
cell.backgroundColor = [UIColor clearColor];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", nutrivals.type]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", nutrivals.value]];
return cell;
}
@end
在JSON完成方法中放置[[KGModal sharedInstance] showWithContentView:_tv andAnimated:YES];
也会崩溃。
崩溃表示EXC_BAD_ACCESS;
2013-08-30 19:13:56.634 xapp[91159:a0b] *** -[itemNutriModal tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0xd8add90
----在第一个cellForRowAtIndexPath调用上堆栈跟踪(有效)----
2013-08-30 19:34:00.229 xapp[91619:a0b] Stack trace : (
0 xapp 0x0002dec3 -[itemNutriModal showNutri] + 483
1 xapp 0x0001a60f -[favouritesTableViewController tableView:accessoryButtonTappedForRowWithIndexPath:] + 175
2 UIKit 0x009baa25 -[UITableView _accessoryButtonAction:] + 310
3 libobjc.A.dylib 0x01b7c874 -[NSObject performSelector:withObject:withObject:] + 77
4 UIKit 0x008e424c -[UIApplication sendAction:to:from:forEvent:] + 108
5 UIKit 0x008e41d8 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
6 UIKit 0x009daa5d -[UIControl sendAction:to:forEvent:] + 66
7 UIKit 0x009dae20 -[UIControl _sendActionsForEvents:withEvent:] + 577
8 UIKit 0x009da0cf -[UIControl touchesEnded:withEvent:] + 641
9 UIKit 0x00c5440f _UIGestureRecognizerUpdate + 7166
10 UIKit 0x00920e9a -[UIWindow _sendGesturesForEvent:] + 1291
11 UIKit 0x00921dba -[UIWindow sendEvent:] + 1030
12 UIKit 0x008f5b86 -[UIApplication sendEvent:] + 242
13 UIKit 0x008e035f _UIApplicationHandleEventQueue + 11421
14 CoreFoundation 0x01d7396f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
15 CoreFoundation 0x01d732fb __CFRunLoopDoSources0 + 235
16 CoreFoundation 0x01d903ce __CFRunLoopRun + 910
17 CoreFoundation 0x01d8fbf3 CFRunLoopRunSpecific + 467
18 CoreFoundation 0x01d8fa0b CFRunLoopRunInMode + 123
19 GraphicsServices 0x03125a27 GSEventRunModal + 192
20 GraphicsServices 0x0312584e GSEventRun + 104
21 UIKit 0x008e2f0b UIApplicationMain + 1225
22 xapp 0x0001313d main + 141
23 libdyld.dylib 0x03d05725 start + 0
)
----从JSON完成(异常)调用第二个cellForRowAtIndexPath调用的堆栈跟踪----
2013-08-30 19:34:00.332 xapp[91619:a0b] Stack trace : (
0 xapp 0x0002e1a6 __27-[itemNutriModal showNutri]_block_invoke + 230
1 xapp 0x0000b3d0 __58-[JSONModel(Networking) initFromURLWithString:completion:]_block_invoke_2 + 112
2 libdispatch.dylib 0x03a60818 _dispatch_call_block_and_release + 15
3 libdispatch.dylib 0x03a754b0 _dispatch_client_callout + 14
4 libdispatch.dylib 0x03a62a0e _dispatch_after_timer_callback + 98
5 libdispatch.dylib 0x03a754b0 _dispatch_client_callout + 14
6 libdispatch.dylib 0x03a6be6b _dispatch_source_latch_and_call + 150
7 libdispatch.dylib 0x03a66443 _dispatch_source_invoke + 411
8 libdispatch.dylib 0x03a636f6 _dispatch_main_queue_callback_4CF + 228
9 CoreFoundation 0x01e4fb6e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
10 CoreFoundation 0x01d907eb __CFRunLoopRun + 1963
11 CoreFoundation 0x01d8fbf3 CFRunLoopRunSpecific + 467
12 CoreFoundation 0x01d8fa0b CFRunLoopRunInMode + 123
13 GraphicsServices 0x03125a27 GSEventRunModal + 192
14 GraphicsServices 0x0312584e GSEventRun + 104
15 UIKit 0x008e2f0b UIApplicationMain + 1225
16 xapp 0x0001313d main + 141
17 libdyld.dylib 0x03d05725 start + 0
)
答案 0 :(得分:0)
一旦分配了数据源/委托,tableview就会查询数据。您的JSON回调可能还没有完成运行。您需要检查它是否已完成,如果没有,则返回0行。如果在JSON查询实际完成之前返回8行,那么您将崩溃。检查_feed在numberOfRowsInSection中是否包含任何数据,如果没有,则返回0.
答案 1 :(得分:0)
似乎这是KGModal的问题 - 分配给KGModal对象时视图/控制器的dealloc意味着它不能再被reloadData命令操纵。
我绕过这个方法是制作我自己的自定义UIScrollView,在分配到模态窗口之前填充它。