重新加载collectionview的数据

时间:2014-02-06 16:54:58

标签: ios uicollectionview segue modalviewcontroller

我有2个ViewControllers

ViewControllerWithCollectionView( FIRST )和ModalViewControllerToEditCellContent( SECOND

我从 FIRST 转换为 SECOND 模态。编辑单元格。返回。

在解除 SECOND 控制器后,编辑的单元格在我打电话之前不会更新 [collection reloadData];在某处手动。

试图把它放在viewWillAppear:animated:中,当我检查日志时,它没有被调用(在解除 SECOND 之后)

我尝试过各种各样的解决方案,但我不能刹车(也许我太累了)。我觉得我错过了一些基本的东西。

编辑关闭按钮

- (IBAction)modalViewControllerDismiss  
    {
        self.sticker.text = self.text.text; //using textFields text
        self.sticker.title = self.titleText.text;// title

        //tried this also
        CBSStickerViewController *pvc = (CBSStickerViewController *)self.stickerViewController; 
        //tried passing reference of **FIRST** controller
        [pvc.cv reloadData];//called reloadData
        //nothing

        [self dismissViewControllerAnimated:YES completion:^{}];
    }

1 个答案:

答案 0 :(得分:1)

很难从发布的代码中判断出传递给第二个视图控制器的指针有什么问题。您还应该能够在第二个视图控制器中引用self.presentingViewController。无论哪种方式,更漂亮的设计是为第一个视图控制器找到一种方法来了解已经进行了更改并更新了自己的视图。

有几种方法,但我会建议代理模式。第二个视图控制器可以设置为让第一个视图控制器为它工作,即重新加载表视图。以下是几乎代码的外观:

// SecondVc.h

@protocol SecondVcDelegate;

@interface SecondVC : UIViewController

@property(weak, nonatomic) id<SecondVcDelegate>delegate;  // this will be an instance of the first vc

// other properties

@end

@protocol SecondVcDelegate <NSObject>

- (void)secondVcDidChangeTheSticker:(SecondVc *)vc;

@end

现在第二个vc使用它来要求第一个vc为它工作,但是第二个vc对于第一个vc实现的细节仍然相当愚蠢。我们这里没有引用第一个vc的UITableView,或者它的任何视图,我们也没有告诉任何表重新加载。

// SecondVc.m

- (IBAction)modalViewControllerDismiss {

    self.sticker.text = self.text.text; //using textFields text
    self.sticker.title = self.titleText.text;// title

    [self.delegate secondVcDidChangeTheSticker:self];
    [self dismissViewControllerAnimated:YES completion:^{}];
}

现在必须做的就是让第一个vc做必须成为代表的事情:

// FirstVc.h

#import "SecondVc.h"

@interface FirstVc :UIViewController <SecondVcDelegate>  // declare itself a delegate

// etc.

// FirstVc.m

// wherever you decide to present the second vc
- (void)presentSecondVc {

    SecondVc *secondVc = // however you do this now, maybe get it from storyboard?
    vc.delegate = self;  // that's the back pointer you were trying to achieve

    [self presentViewController:secondVc animated:YES completion:nil];
}

最后,妙语。实现委托方法。在这里,您可以通过重新加载表视图来完成第二个vc想要的工作

- (void) secondVcDidChangeTheSticker:(SecondVc *)vc {

    [self.tableView reloadData];  // i think you might call this "cv", which isn't a terrific name if it's a table view
}
相关问题