Ios UITableView目标-c未正确填充

Ios UITableView目标-c未正确填充,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我尝试在UITableView中显示单元格,但除非使用viewDidLoad,否则不会显示新添加的单元格 以下是完整的列表: 如果我进入New Note屏幕并返回,这很好,但是如果通过侧菜单访问此屏幕,则注释标题New将消失,直到我单击New Note按钮并返回 以下是此页面的代码: - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *leftButtonsArray = [self.navigationIte

我尝试在
UITableView
中显示单元格,但除非使用
viewDidLoad
,否则不会显示新添加的单元格

以下是完整的列表: 如果我进入
New Note
屏幕并返回,这很好,但是如果通过侧菜单访问此屏幕,则注释标题
New
将消失,直到我单击
New Note
按钮并返回

以下是此页面的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray *leftButtonsArray = [self.navigationItem.leftBarButtonItems mutableCopy];
    UIBarButtonItem *newNoteButton = [[UIBarButtonItem alloc] initWithTitle:@"New Note" style:UIBarButtonItemStyleBordered target:self action:@selector(addNote:)];
    [leftButtonsArray addObject:newNoteButton];
    self.navigationItem.leftBarButtonItems = leftButtonsArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.notes = [NSMutableArray array];

    ontracAppDelegate *delegate = (ontracAppDelegate*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *moc = [delegate managedObjectContext];

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Notes"];
    [request setReturnsObjectsAsFaults:NO];
    NSError *error = nil;

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];
    [request setSortDescriptors:@[sortDescriptor]];

    NSArray *results = [moc executeFetchRequest:request error:&error];
    NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];

    NSArray *sortDescriptors = [NSArray arrayWithObject:sd];
    NSArray *sortedArray = [results sortedArrayUsingDescriptors:sortDescriptors];

    NSNumber * NOTEID = @"noteID";
    NSString * PACKID = @"pack_id";
    NSString * NOTE   = @"note";
    NSString * CREATED  = @"created";
    NSString * UPDATED = @"updated";

    NSDictionary * dict;

    [self.notes removeAllObjects];

    for(Notes *note in results){

        if(self.dataObject.pack_id == [note.dataObject.dataPack.pack_id integerValue]){
            dict = [NSDictionary dictionaryWithObjectsAndKeys:
                note.noteID, NOTEID,
                note.pack_id, PACKID,
                note.note, NOTE,
                note.created, CREATED,
                note.updated, UPDATED,
                nil];

            if((note.created == nil || note.updated == nil || note.noteID == nil)){
                [moc deleteObject:note];
            }else{
                [self.notes addObject:dict];
            }
        }
    }

    NSLog(@"mArray %@", self.notes);

 }
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:          (NSInteger)section
{
    NSLog(@"notes count: %lu", (unsigned long)[self.notes count]);
    return [self.notes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"myCell"];
    }

    NSMutableDictionary *d = [self.notes objectAtIndex:indexPath.row];
    //NSNumber *noteID = [d valueForKey:@"noteID"];
    NSString *note = [d valueForKey:@"note"];
    NSString *created = [d valueForKey:@"created"];
    NSString *updated = [d valueForKey:@"updated"];

    cell.textLabel.text = note;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Date Created: %@ | Last Updated %@", created, updated];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *d = [self.notes objectAtIndex:indexPath.row];
    NSNumber *noteID = [d valueForKey:@"noteID"];
    NID = noteID;

    ontracSuperClassViewController *viewController = [[ontracNotesViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}
-(IBAction)addNote:(id)sender{
    NID = [NSNumber numberWithInteger:-1];
    ontracSuperClassViewController *viewController = [[ontracNotesViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

无论何时在添加新节点后在该函数中添加新注释,只要重新加载tableview即可

无论何时在添加新节点后在该函数中添加新注释,只要重新加载tableview即可

当您用最新更新的数据填充数组时,您必须重新加载
tableview
以显示最新的数据。因此,如果您的
self.notes
视图中正确打印更新后的数据将出现在
NSLog
中,那么您只需
重新加载
您的
表视图
即可

   [tableView reloadData];

如果您没有在
self.notes
中获得更新的数据,请先解决该问题,然后重新加载
tableview

用最新更新的数据填充数组时,必须重新加载
tableview
以显示最新的数据。因此,如果您的
self.notes
视图中正确打印更新后的数据将出现在
NSLog
中,那么您只需
重新加载
您的
表视图
即可

   [tableView reloadData];

如果您没有在
self.notes
中获得更新的数据,请先解决该问题,然后重新加载
tableview

viewDidLoad()方法中添加此行

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.Methodtablereloadata(_:)), name:"NotificationIdentifier", object: nil)


-(void) Methodtablereloadata(notification: NSNotification){
        //Take Action on Notification
         self.notes = [NSMutableArray array];

ontracAppDelegate *delegate = (ontracAppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [delegate managedObjectContext];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Notes"];
[request setReturnsObjectsAsFaults:NO];
NSError *error = nil;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];

NSArray *results = [moc executeFetchRequest:request error:&error];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];

NSArray *sortDescriptors = [NSArray arrayWithObject:sd];
NSArray *sortedArray = [results sortedArrayUsingDescriptors:sortDescriptors];

NSNumber * NOTEID = @"noteID";
NSString * PACKID = @"pack_id";
NSString * NOTE   = @"note";
NSString * CREATED  = @"created";
NSString * UPDATED = @"updated";

NSDictionary * dict;

[self.notes removeAllObjects];

for(Notes *note in results){

    if(self.dataObject.pack_id == [note.dataObject.dataPack.pack_id integerValue]){
        dict = [NSDictionary dictionaryWithObjectsAndKeys:
            note.noteID, NOTEID,
            note.pack_id, PACKID,
            note.note, NOTE,
            note.created, CREATED,
            note.updated, UPDATED,
            nil];

        if((note.created == nil || note.updated == nil || note.noteID == nil)){
            [moc deleteObject:note];
        }else{
            [self.notes addObject:dict];
        }
    }
  }
  [YOURTABLE reloadData];
 }
下一个添加新注释的ViewController。添加注释后,只需为重新加载表格写一行即可

 NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
请参阅此链接


viewDidLoad()方法中添加此行

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.Methodtablereloadata(_:)), name:"NotificationIdentifier", object: nil)


-(void) Methodtablereloadata(notification: NSNotification){
        //Take Action on Notification
         self.notes = [NSMutableArray array];

ontracAppDelegate *delegate = (ontracAppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [delegate managedObjectContext];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Notes"];
[request setReturnsObjectsAsFaults:NO];
NSError *error = nil;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];

NSArray *results = [moc executeFetchRequest:request error:&error];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];

NSArray *sortDescriptors = [NSArray arrayWithObject:sd];
NSArray *sortedArray = [results sortedArrayUsingDescriptors:sortDescriptors];

NSNumber * NOTEID = @"noteID";
NSString * PACKID = @"pack_id";
NSString * NOTE   = @"note";
NSString * CREATED  = @"created";
NSString * UPDATED = @"updated";

NSDictionary * dict;

[self.notes removeAllObjects];

for(Notes *note in results){

    if(self.dataObject.pack_id == [note.dataObject.dataPack.pack_id integerValue]){
        dict = [NSDictionary dictionaryWithObjectsAndKeys:
            note.noteID, NOTEID,
            note.pack_id, PACKID,
            note.note, NOTE,
            note.created, CREATED,
            note.updated, UPDATED,
            nil];

        if((note.created == nil || note.updated == nil || note.noteID == nil)){
            [moc deleteObject:note];
        }else{
            [self.notes addObject:dict];
        }
    }
  }
  [YOURTABLE reloadData];
 }
下一个添加新注释的ViewController。添加注释后,只需为重新加载表格写一行即可

 NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
请参阅此链接


NSLog(@“mArray%@”,self.notes)之后的,执行[yourTableView重新加载数据]
,或使用
NSFetchedResultsController
?正如您所说,您已经在新的viewcontroller中添加了一个新的注释,然后使用nsnotificationcenter addobserver并从新的viewcontroller调用该方法,并使用新值更新数组并重新加载表格当您推到第二个view controller时,您使用什么返回到这里?在
NSLog之后(@“mArray%@”,self.notes);
,do
[yourTableView重新加载数据];
,或使用
NSFetchedResultsController
?正如您所说,您已经在新的viewcontroller中添加了一个新的注释,然后使用nsnotificationcenter addobserver,并从新的viewcontroller调用该方法,并使用新值更新您的数组,并在推到第二个view controller时重新加载表格,您使用什么回到这里?