【问题标题】:How can I edit over a NSString that is saved in an Array?如何编辑保存在数组中的 NSString?
【发布时间】:2013-03-24 02:28:14
【问题描述】:

我将多个字符串保存到 NSMutableArray 中,然后将每个字符串设置到单独的表格视图单元格中。

如果选择了一个单元格,该字符串将显示在另一个视图中。

我希望能够编辑字符串并再次保存。

解决这个问题的最佳方法是什么?

这是我的存档代码:

- (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];
        NSMutableArray * tempNotePaths = [NSMutableArray arrayWithObject:tempNotePath];
        [_savedNotesViewController.noteTableView insertRowsAtIndexPaths:tempNotePaths withRowAnimation:NO];

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

    }

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

}

【问题讨论】:

  • 如果它是一个 NSString,你不能“编辑”它;你能做的最多就是用可变数组中的不同字符串替换它。如果它是一个 NSMutableString,那么无论对它做什么,正如任何其他引用所看到的,都是对它完成的,就像可变数组所看到的那样。

标签: objective-c uiview nsstring nsmutablearray uitextview


【解决方案1】:

你可以使用 NSMutableArray 的方法[array replaceObjectAtIndex:index withObject:@"newString"]

【讨论】:

  • 检查文本是否等于自身的正确方法是什么?还是我需要这样做?
  • @LukeIrvin- 你可以通过 if(text1 isEqualToString:text2) 比较文本
猜你喜欢
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多