更新父视图控制器的数据源

发布于 2025-01-02 12:10:11 字数 379 浏览 2 评论 0原文

我有三个级别的 ViewController,它们从整个类别深入到该类别中的主题,再到该主题中的引用。

视图控制器被称为:

1   CategoryViewController
2   SubjectViewController
3   QuoteViewController

数据库表“SUBJECT”具有类别和主题列。

当我在SubjectViewController 中时,我可以编辑或删除主题和类别。

我希望能够更新父视图控制器 CategoryViewController 的数据源并“刷新”其 tableView,因此当我导航回它时,我已经看到了显示的表中反映的更改。

解决这个问题的正确方法是什么?

I have three levels of ViewControllers that drill down from an overall category to a subject within that category to a quote within that subject.

The view controllers are called:

1   CategoryViewController
2   SubjectViewController
3   QuoteViewController

The database table "SUBJECT" has category and subject column.

When I am in the SubjectViewController I can edit or delete subjects and categories.

I want to be able to update the data source of the parent view controller, CategoryViewController and "refresh" its tableView so when I navigate back to it, I already see the changes reflected in the displayed table.

What is the correct way to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名的好友 2025-01-09 12:10:11

我认为授权是完成这项任务最合适的方法。您应该创建一个名为 SubjectViewControllerDelegate 的新协议,并添加一些方法,例如:

- (void)subjectViewController:(SubjectViewController*)controller didEditSubject:(Subject*)subject;
- (void)subjectViewController:(SubjectViewController*)controller didDeleteSubject:(Subject*)subject;

SubjectViewController 有一个委托属性来保持对其委托的弱引用。
每当删除或编辑数据时,它都会调用委托方法。

CategoryViewController 实现此协议,并在将其推送到导航控制器之前将其自身设置为SubjectViewController 实例的委托。在此方法中,您可以刷新所有表,也可以仅更新已更改的行。

更新:

假设您在 CategoryViewController 中只有一个部分,并将主题保存在 NSMutableArray 中,您可以使用类似于下面的代码来更新表视图的相关部分。

- (void)subjectViewController:(SubjectViewController*)controller didEditSubject:(Subject*)subject{
    NSInteger row;
    NSIndexPath* indexPath;
    NSArray* indexPaths;

    row = [self.subjects indexOfObject:subject];
    indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    indexPaths = [NSArray arrayWithObject:indexPath];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];    
}

- (void)subjectViewController:(SubjectViewController*)controller didDeleteSubject:(Subject*)subject{
    NSInteger row;
    NSIndexPath* indexPath;
    NSArray* indexPaths;

    row = [self.subjects indexOfObject:subject];
    indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    indexPaths = [NSArray arrayWithObject:indexPath];
    [self.subjects removeObjectAtIndex:row];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}

I think delegation is the most appropriate method for this task. You should create a new protocol called SubjectViewControllerDelegate and add some methods like:

- (void)subjectViewController:(SubjectViewController*)controller didEditSubject:(Subject*)subject;
- (void)subjectViewController:(SubjectViewController*)controller didDeleteSubject:(Subject*)subject;

SubjectViewController has a delegate property to keep weak reference to its delegate.
It calls the delegate methods whenever a data is deleted or edited.

CategoryViewController implements this protocol and sets itself as the delegate of SubjectViewController instance before pushing it to the navigation controller. In this methods you can either refresh the all table, or just update the rows that are changed.

UPDATE:

Assuming that you have only one section in CategoryViewController and keep the subjects in an NSMutableArray, you can use the codes similar to below for updating the relevant part of the table view.

- (void)subjectViewController:(SubjectViewController*)controller didEditSubject:(Subject*)subject{
    NSInteger row;
    NSIndexPath* indexPath;
    NSArray* indexPaths;

    row = [self.subjects indexOfObject:subject];
    indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    indexPaths = [NSArray arrayWithObject:indexPath];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];    
}

- (void)subjectViewController:(SubjectViewController*)controller didDeleteSubject:(Subject*)subject{
    NSInteger row;
    NSIndexPath* indexPath;
    NSArray* indexPaths;

    row = [self.subjects indexOfObject:subject];
    indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    indexPaths = [NSArray arrayWithObject:indexPath];
    [self.subjects removeObjectAtIndex:row];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
时光无声 2025-01-09 12:10:11

使用 UIViewControllers

@property(nonatomic, readonly) UIViewController *parentViewController

你可以做类似的事情

CategoryViewController *cvc = (id)self.parentViewController
if ([cvc isKindOfClass:[CategoryViewController class]]) {
  // call stuff on cvc, like a table view's -reloadData
}

或者你可以引入一个通知类型并观察它。

Use the UIViewControllers

@property(nonatomic, readonly) UIViewController *parentViewController

You could do something like that

CategoryViewController *cvc = (id)self.parentViewController
if ([cvc isKindOfClass:[CategoryViewController class]]) {
  // call stuff on cvc, like a table view's -reloadData
}

Or you could introduce a notification type and observe that.

阪姬 2025-01-09 12:10:11

您可以像这样从活动视图控制器(例如SubjectViewController)发送全局通知:

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

然后在要刷新的视图控制器中监听它,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataChanged) name:@refresh_data" object:nil];

- (void)dataChanged {
// perform the refreshing 

}

You could send a global notification from the active ViewController (for example SubjectViewController) like this:

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

and then listen for it in the view controllers that you with to refresh like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataChanged) name:@refresh_data" object:nil];

- (void)dataChanged {
// perform the refreshing 

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文