0

My app is only crashing in a specific way. Here's a break down of what's going on.

I can type in text in a UITextView and tap a button that saves the text and adds a row to a UITableView in another UIViewController. I can then tap on the desired cell from the UITableView and that UIViewController will dismiss and the text will appear again on the main UIViewController.

I have another button that simply clears out the UITextView so I can type in new text.

If I view the text from an added row and then tap the "Add" button to input new text and then tap the "Save" button my app crashes.

Here's some of the code:

didSelectRow Code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
     //Setting the text stored in an array into a NSString here
    _displayString = [_savedNoteArray objectAtIndex:indexPath.row];

    [self dismissViewControllerAnimated:YES completion:nil];
} 

save button code:

- (IBAction)saveNote
{
    if (_noteView.aTextView.text == nil)
    {
        [_noteArray addObject:@""];

        Note * tempNote = [[Note alloc] init];
        _note = tempNote;

        [_savedNotesViewController.savedNoteArray addObject:tempNote];
        NSIndexPath * tempNotePath = [NSIndexPath indexPathForRow: [_savedNotesViewController.savedNoteArray count]-1 inSection:0];
        NSArray * tempNotePaths = [NSArray arrayWithObject:tempNotePath];
    [_savedNotesViewController.noteTableView insertRowsAtIndexPaths:tempNotePaths withRowAnimation:NO];

       [[NSNotificationCenter defaultCenter] postNotificationName:@"AddNote" object:nil];

    }
    else
    {
        [_noteArray addObject:self.noteView.aTextView.text];

        Note * tempNote = [[Note alloc] init];
       _note = tempNote;

        [_savedNotesViewController.savedNoteArray addObject:tempNote];
        NSIndexPath * tempNotePath = [NSIndexPath indexPathForRow:[_savedNotesViewController.savedNoteArray count]-1 inSection:0];
        NSArray * tempNotePaths = [NSArray arrayWithObject:tempNotePath];

        //**** This is where the app is crashing *****
        [_savedNotesViewController.noteTableView insertRowsAtIndexPaths:tempNotePaths withRowAnimation:NO];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"AddNote" object:nil];

    }

    Note * myNote = [Note sharedNote];
    myNote.noteOutputArray = _noteArray;

}

add butt code (makes a new UITextView):

- (IBAction)addButtonTapped
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddNote" object:nil];
}

in my viewWillAppear to show the selected row text I do this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.noteView.aTextView.text = _savedNotesViewController.displayString;
}

note code (singleton class):

 static Note * sharedNote = nil;

 - (id)initWithNote:(NSString *)newNote
 {
    self = [super init];

    if (nil != self)
    {
        self.note = newNote;
    }

    return self;
}

+ (Note *) sharedNote
{
    @synchronized(self)
    {
        if (sharedNote == nil)
        {
            sharedNote = [[self alloc] init];
        }
    }

    return sharedNote;
}

When the app crashes I get this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Note isEqualToString:]: unrecognized selector sent to instance 0x8875f20'

Stepping through my code, the text is being added to the array, but when it comes time to insertRowsAtIndexPaths the app blows up.

Any advice is much appreciated!

* EDIT // TableView Code **

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_savedNoteArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    //I have a feeling this could be where an issue is. 
    NSString * cellString = [_savedNoteArray objectAtIndex:indexPath.row];
    cell.textLabel.text = cellString;

    return cell;
}
4

1 回答 1

1

一个潜在的问题(可能不是您的崩溃,但无论如何都会导致问题)是您将 Note 对象存储在savedNoteArray但您试图将它们用作字符串(您的代码如下):

//Setting the text stored in an array into a NSString here
_displayString = [_savedNoteArray objectAtIndex:indexPath.row];

然后将该 displayString 分配给 UITextView 的文本属性(应该是 NSString*):

self.noteView.aTextView.text = _savedNotesViewController.displayString;

这个问题的简短形式可以概括为......

Note *note = [[NSNote alloc] init];
[array addObject:note];
textView.text = array[0];

这显然会导致问题。您基本上是在将“Note”对象分配给应该是字符串的东西。

cellForRowAtIndexPath:这可能会导致您在表视图数据源的方法中遇到的崩溃。您是否也在使用 Note 对象,或者您是否正确地将 NSStrings 分配给视图?

于 2013-03-05T06:48:52.443 回答