当推送到详细视图时,应用程序崩溃

问题描述:

我正在使用包含标题和说明的单元格处理表格视图应用程序。当点击单元格时,您将移动到显示相同标题和说明的详细视图,以及长文本(从txt文件加载)。当推送到详细视图时,应用程序崩溃

这个工作在几天前,但从那以后我做了一些改变,似乎已经搞砸了。现在,当我点击一个单元格时,该应用程序崩溃。

这是我的代码:

AlfaTableViewController.m

#import "AlfaTableViewController.h" 
#import "TableCell.h" 
#import "DetailViewController.h" 

@interface AlfaTableViewController() { 
    NSDictionary *Title; 
    NSDictionary *Description; 
    NSDictionary *Text; 
    NSArray *songSectionTitles; 
    } 

@end 

@implementation AlfaTableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    Title = @{@"A" : @[@"Adventstid", @"Albertina", @"Alfabetsvisan"], 
      @"B" : @[@"Badvisa för småbjörnar", @"Bagaren"], 
      @"C" : @[@"Citron"]}; 

    Description = @{@"A" : @[@"Text: Nisse Lamm", @"Text: Mora Träsk", @"Text: Alice Tegner"], 
        @"B" : @[@"Text: Lennart Hellsing", @"Text: Alice Tegner"], 
        @"C" : @[@"Text: Dockmormor"]}; 

    Text = @{@"A" : @[@"adventstid", @"albertina", @"alfabetsvisan"], 
      @"B" : @[@"badvisa", @"bagaren"], 
      @"C" : @[@"citron"]}; 

    songSectionTitles = [[Title allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [songSectionTitles count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [songSectionTitles objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSString *sectionTitle = [songSectionTitles objectAtIndex:section]; 
    NSArray *sectionName = [Title objectForKey:sectionTitle]; 
    return [sectionName count]; 
} 


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

    // Configure the cell... 
    NSString *sectionTitle = [songSectionTitles objectAtIndex:indexPath.section]; 
    NSArray *sectionName = [Title objectForKey:sectionTitle]; 
    NSString *name = [sectionName objectAtIndex:indexPath.row]; 
    NSArray *sectionDescription = [Description objectForKey:sectionTitle]; 
    NSString *description = [sectionDescription objectAtIndex:indexPath.row]; 

    cell.TitleLabel.text = name; 
    cell.DescriptionLabel.text = description; 

    return cell; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) { 
      DetailViewController *detailviewcontroller = segue.destinationViewController; 

     detailviewcontroller.DetailModal = @[Title, Description, Text]; 
    } 
} 

@end 

DetailViewController.m

#import "DetailViewController.h" 

@interface DetailViewController() 

@end 

@implementation DetailViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    _TitleLabel.text = _DetailModal[0]; 
    _DescriptionLabel.text = _DetailModal[1]; 
    _TextView.text =_DetailModal[2]; 

    self.navigationItem.title = _DetailModal[0]; 

    NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[2] ofType:@"txt"]; 
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
    self.TextView.text = content; 

} 

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

@end 

而崩溃日志

2016-11-03 17:17:22.637 Barntexter[11893:936314] -[__NSDictionaryI length]: unrecognized selector sent to instance 0x7fc813f07e10 
2016-11-03 17:17:22.641 Barntexter[11893:936314] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0x7fc813f07e10' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010a0fbd85 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000109b6fdeb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010a104d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x000000010a04acfa ___forwarding___ + 970 
    4 CoreFoundation      0x000000010a04a8a8 _CF_forwarding_prep_0 + 120 
    5 CoreFoundation      0x000000010a0e58cd isEqualToString + 61 
    6 CoreFoundation      0x000000010a0e5a5c -[NSTaggedPointerString isEqualToString:] + 28 
    7 UIKit        0x000000010a724df5 -[UILabel _setText:] + 82 
    8 Barntexter       0x00000001096651e5 -[DetailViewController viewDidLoad] + 149 
    9 UIKit        0x000000010a64c984 -[UIViewController loadViewIfRequired] + 1198 
    10 UIKit        0x000000010a65293b -[UIViewController __viewWillAppear:] + 120 
    11 UIKit        0x000000010a682750 -[UINavigationController _startCustomTransition:] + 1203 
    12 UIKit        0x000000010a692b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712 
    13 UIKit        0x000000010a693d0b -[UINavigationController __viewWillLayoutSubviews] + 57 
    14 UIKit        0x000000010a842503 -[UILayoutContainerView layoutSubviews] + 248 
    15 UIKit        0x000000010a56c980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 
    16 QuartzCore       0x000000010e593c00 -[CALayer layoutSublayers] + 146 
    17 QuartzCore       0x000000010e58808e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    18 QuartzCore       0x000000010e587f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    19 QuartzCore       0x000000010e57c3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 
    20 QuartzCore       0x000000010e5aa086 _ZN2CA11Transaction6commitEv + 486 
    21 UIKit        0x000000010a4de19b _afterCACommitHandler + 174 
    22 CoreFoundation      0x000000010a020c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    23 CoreFoundation      0x000000010a020ba7 __CFRunLoopDoObservers + 391 
    24 CoreFoundation      0x000000010a0167fb __CFRunLoopRun + 1147 
    25 CoreFoundation      0x000000010a0160f8 CFRunLoopRunSpecific + 488 
    26 GraphicsServices     0x000000010d97bad2 GSEventRunModal + 161 
    27 UIKit        0x000000010a4b1f09 UIApplicationMain + 171 
    28 Barntexter       0x00000001096659ff main + 111 
    29 libdyld.dylib      0x000000010c8d592d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+1

Crash log please,否则很难找到问题 –

+0

这段代码有很多地方可能会失败。所以请更新与日志错误的问题 – EridB

+0

detailviewcontroller.DetailModal = @ [Title,Description,Text];您正在将字典传递到详细模式,并且当您正在设置_TitleLabel.text = _DetailModal [0]; _DescriptionLabel.text = _DetailModal [1]; _TextView.text = _DetailModal [2]; ,你正在分配字典到文本,这就是为什么它在这里崩溃。 – Darshana

在你下面的鳕鱼E:

detailviewcontroller.DetailModal = @[Title, Description, Text]; 

你逝去的字典到细节模式,而当你设置

_TitleLabel.text = _DetailModal[0]; 
_DescriptionLabel.text = _DetailModal[1]; 
_TextView.text =_DetailModal[2]; 

要指定词典的文本,这就是为什么它的崩溃在这里。

试试这个。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

     if ([[segue identifier] isEqualToString:@"ShowDetails"]) { 
       DetailViewController *detailviewcontroller = segue.destinationViewController; 

       NSIndexPath *indexPath= [self.tableView indexPathForSelectedRow]; 

NSString *sectionTitle = [songSectionTitles objectAtIndex:indexPath.section]; 
    NSArray *sectionName = [Title objectForKey:sectionTitle]; 
    NSString *name = [sectionName objectAtIndex:indexPath.row]; 
    NSArray *sectionDescription = [Description objectForKey:sectionTitle]; 
    NSString *description = [sectionDescription objectAtIndex:indexPath.row]; 
detailviewcontroller.DetailModal = @[sectionName, name ,description ] 
     } 
    } 
+0

没有工作:(我得到的错误:“没有可见的@interface为'NSDictionary'声明选择器'objectAtIndex:'”我更新了我的问题与崩溃日志。帮助:) –

+0

@JohanGrip我已经更新了答案。请试试这个。 。 – Darshana

+0

不幸的是仍然无法正常工作。错误消失了,但它像以前一样崩溃。它在我尝试插入分区分隔线之前就工作过了,所以我想我必须追溯到没有分隔线的情况。无论如何,谢谢@iOS_developer –

的问题是,你正在创建DetailModal字典的数组,但后来内部的DetailViewController你使用它作为的数组。

在此代码:

_TitleLabel.text = _DetailModal[0]; 
_DescriptionLabel.text = _DetailModal[1]; 
_TextView.text =_DetailModal[2]; 

self.navigationItem.title = _DetailModal[0]; 

NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[2] ofType:@"txt"]; 

_DetailModal[0]_DetailModal[1]_DetailModal[2]字符串,这就是为什么你的应用程序崩溃。 他们是字典:你需要从字典中获得正确的文本,然后你可以将其分配给标签。

+0

我只想补充一点,虽然这可能是也可能不是确切的问题(对于我来说阅读过多的代码!),但问题非常明显,字典正被分配给“文本”属性,因为这就是崩溃所说的:' - [__ NSDictionaryI length]:无法识别的选择器被发送到实例0x7fc813f07e10''(在'setExt'内的'isEqualToString'内)。所以这可能是完全正确的,或者至少应该足够接近,让你在那里。 –