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

时间:2016-11-03 13:48:39

标签: ios objective-c xcode uitableview

我正在使用包含标题和说明的单元格的表格视图应用程序。点击单元格时,您将移动到显示相同标题和描述的详细视图,以及长文本(从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) 

2 个答案:

答案 0 :(得分:1)

问题是您正在创建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] 不是字符串,这就是您的应用崩溃的原因。 它们是字典:您需要从字典中获取正确的文本,然后才能将其分配给标签。

答案 1 :(得分:0)

在以下代码中:

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 ]
        }
    }