繁体   English   中英

尝试从NSMutableArray填充UITableView时,应用崩溃

[英]App crashing when trying to populate a UITableView from NSMutableArray

我似乎无法弄清楚为什么在向表中添加对象时我的应用程序崩溃了。 我使用了此( http://www.theappcodeblog.com/2011/05/09/parsing-xml-in-an-iphone-app-tutorial/ )教程来解析XML,这样做确实不错,但没有添加数据从我解析的XML到UITableView。

这是我正在使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"starting table");

    static NSString *cellid = @"Cell";
    row *curname = [[xpar worker] objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid]autorelease];
    }

    picframe = CGRectMake(0, 0, 100, 100);
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:picframe];
    imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", curname.pic]];
    [cell.contentView addSubview:imgview];

    nameframe = CGRectMake(105, 50, 50, 20);
    namecon = [[UILabel alloc] initWithFrame:nameframe];
    namecon.tag = 0011;
    namecon.numberOfLines = 1;
    namecon.font = [UIFont boldSystemFontOfSize:12.0];
    [cell.contentView addSubview:namecon];

    jobframe = CGRectMake(165, 75, 50, 20);
    jobcon = [[UILabel alloc] initWithFrame:jobframe];
    jobcon.tag = 0012;
    jobcon.numberOfLines = 1;
    jobcon.font = [UIFont systemFontOfSize:12.0];
    [cell.contentView addSubview:jobcon];

    majframe = CGRectMake(165, 25, 50, 20);
    majcon = [[UILabel alloc] initWithFrame:majframe];
    majcon.tag = 0013;
    majcon.numberOfLines = 1;
    majcon.font = [UIFont systemFontOfSize:12.0];
    [cell.contentView addSubview:majcon];

    namecon = (UILabel*)[cell.contentView viewWithTag:0011];
    namecon.text = [curname name];

    NSLog(@"%@", namecon.text);

    jobcon = (UILabel*)[cell.contentView viewWithTag:0012];
    jobcon.text = [curname job];

    majcon = (UILabel*)[cell.contentView viewWithTag:0013];
    majcon.text = [curname major];

    return cell;
}

我在代码中使用带有UITableView的情节提要和带有标识符“ Cell”的TableViewCell。 UIViewController的类是上面的代码的.m文件,UITableView使用的是动态原型,UITableView的dataSource和Delegate Outlets是上面的代码的.m文件,.m文件的Outlet链接到UITableView。

我对Objective-C和iOS开发人员也很陌生,所以如果这是一个很明显的答案,对不起。 我尝试了很多不同的尝试,都无济于事。 如果我的代码中还有其他部分,我应该发布,我会尽快进行。

谢谢!

编辑:崩溃后登录:

2014-04-07 13:18:17.383 LabWorker[80097:60b] -[workersViewController
tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x10c3d4a60
2014-04-07 13:18:17.386 LabWorker[80097:60b] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[workersViewController 
tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x10c3d4a60'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101acd495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010182c99e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101b5e65d -[NSObject(NSObject)    
doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101abeb85 ___forwarding___ + 453
    4   CoreFoundation                      0x0000000101abe938 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x00000001005eba30 -[UISectionRowData 
heightForRow:inSection:canGuess:] + 234
    6   UIKit                               0x00000001005f2334 -[UITableViewRowData 
rectForRow:inSection:heightCanBeGuessed:] + 582
    7   UIKit                               0x00000001005f2432 -[UITableViewRowData 
rectForGlobalRow:heightCanBeGuessed:] + 150
    8   UIKit                               0x00000001005f2dd5 -[UITableViewRowData 
globalRowsInRect:canGuess:] + 359
    9   UIKit                               0x0000000100494531 -[UITableView 
_visibleGlobalRowsInRect:] + 210
    10  UIKit                               0x00000001004937ab -[UITableView 
_updateVisibleCellsNow:] + 881
    11  UIKit                               0x00000001004a5721 -[UITableView layoutSubviews] + 
207
    12  UIKit                               0x0000000100439993 -[UIView(CALayerDelegate) 
layoutSublayersOfLayer:] + 354
    13  QuartzCore                          0x000000010242a802 -[CALayer layoutSublayers] + 151
    14  QuartzCore                          0x000000010241f369 
_ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    15  QuartzCore                          0x000000010241f1ea 
_ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    16  QuartzCore                          0x0000000102392fb8 
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
    17  QuartzCore                          0x0000000102394030 _ZN2CA11Transaction6commitEv + 
394
    18  QuartzCore                          0x000000010239469d 
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    19  CoreFoundation                      0x0000000101a98dc7 
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20  CoreFoundation                      0x0000000101a98d37 __CFRunLoopDoObservers + 391
21  CoreFoundation                      0x0000000101a78522 __CFRunLoopRun + 946
22  CoreFoundation                      0x0000000101a77d83 CFRunLoopRunSpecific + 467
23  GraphicsServices                    0x0000000102f79f04 GSEventRunModal + 161
24  UIKit                               0x00000001003d9e33 UIApplicationMain + 1010
25  LabWorker                           0x0000000100002fa7 main + 103
26  libdyld.dylib                       0x00000001068985fd start + 1
27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

编辑:更多代码/解释:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"sections");
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"rows, %lu", (unsigned long)[[xpar worker] count]);
    return [[xpar worker] count];
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"CGFloat");
    return 100;
}

它运行并为[[xpar worker] count](正确的数目)获取11行,然后运行11次“ CGFloat”方法,然后准确地显示了我上面发布的日志。

编辑:崩溃的地方:

#import <UIKit/UIKit.h>

#import "labAppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
--->     return UIApplicationMain(argc, argv, nil, NSStringFromClass([labAppDelegate class]));
    }
}

它说它崩溃了,位于main.m文件上方的箭头处。

argc = 1

** argv =(字符)'/'

您的日志说它想要tableView:heightForRowAtIndexPath方法

只需实现以下方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

崩溃的原因可能是第一个行的索引无效

row *curname = [[xpar worker] objectAtIndex:indexPath.row];

我没有尝试运行代码。但是您可以记录[xpar worker]的计数并检查它是否大于indexPath.row。您也可以使用防御性代码,例如

if([[xpar worker] count] > indexPath.row)
{
    row *curname = [[xpar worker] objectAtIndex:indexPath.row];
}

但是,请确保通过使用异常断点(如下所示)来确定崩溃的位置:

在此处输入图片说明

该应用程序将在该应用程序崩溃时停止,您可以检查对象的值是否为nil或出于其他任何原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM