【问题标题】:Why is my app crashing during Table View Insert Row?为什么我的应用程序在表格视图插入行期间崩溃?
【发布时间】:2013-03-05 05:37:15
【问题描述】:

我的应用只是以特定方式崩溃。以下是正在发生的事情的细分。

我可以在 UITextView 中输入文本,然后点击一个按钮来保存文本并向另一个 UIViewController 中的 UITableView 添加一行。然后我可以从 UITableView 中点击所需的单元格,UIViewController 将关闭,文本将再次出现在主 UIViewController 上。

我有另一个按钮,可以简单地清除 UITextView,以便我可以输入新文本。

如果我从添加的行中查看文本,然后点击“添加”按钮输入新文本,然后点击“保存”按钮,我的应用程序就会崩溃。

以下是部分代码:

didSelectRow 代码:

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

保存按钮代码:

- (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;

}

添加对接代码(创建一个新的 UITextView):

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

在我的 viewWillAppear 中显示选定的行文本我这样做:

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

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

注释代码(单例类):

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

当应用程序崩溃时,我得到这个:

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

单步执行我的代码,文本被添加到数组中,但是当需要 insertRowsAtIndexPaths 时,应用程序崩溃了。

非常感谢任何建议!

* EDIT // TableView 代码 **

- (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;
}

【问题讨论】:

  • [Note isEqualToString:] 代码在哪里?发送的无法识别的选择器意味着您正在调用未为该对象声明/定义的方法。请添加表格视图控制器的代码以及原因中提到的应用程序崩溃的行。
  • 您在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_noteArray count]; } 中返回正确的值吗?您还没有添加表格视图控制器代码。请添加它。可能会有所帮助

标签: objective-c uitableview uiviewcontroller nsstring uitextview


【解决方案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 分配给视图?

【讨论】:

  • 这可能导致崩溃,标签可能正在检查传入的文本是否与当前文本相同,以查看是否需要重绘。
  • 我首先尝试了这个,它一直有效,直到我在上面发布的问题: NSString * cellString = [_savedNoteArray objectAtIndex:indexPath.row]; cell.textLabel.text = cellString;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多