【问题标题】:UITableView - reloadData from separate classUITableView - 从单独的类中重新加载数据
【发布时间】:2012-06-27 01:48:40
【问题描述】:

我搜索并查看了许多类似的问题,但仍然找不到答案。 如何在不初始化它的另一个实例的情况下引用不同的类? 如何从单独的类中调用“reloadData”以反映 MOC 的数据。 MOC 似乎正在保存,因为我已经通过 NSNotification 验证了它。

Popover 类:

-(void)actionSave:(id)sender {
     MainContent *entityContent =
         [NSEntityDescription insertNewObjectForEntityForName:@"MainContent"
              inManagedObjectContext:self.mDoc.managedObjectContext];
     entityContent.date = [NSDate date];
     entityContent.title = self.titleName.text;

     //Main Question: How do I call the ViewController's function that is in a separate class?
     [ViewController reloadTableView]; //???

     //Separate Question: How do I dismiss this popover from inside of this function?
}

ViewController 类:

-(void)setupTable {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(gridV1, 140, column1_width, 768-170) style:UITableViewStylePlain];
    [self.tableView setBackgroundColor:[UIColor clearColor]];

    self.tableView.autoresizesSubviews = YES;

    self.tableView.delegate = self.tableViewController;
    self.tableView.dataSource = self.tableViewController;
    [self.view addSubview:self.tableView];
}
-(void)reloadTableView{
    [self.tableView reloadData];
}

TableViewController 类:(self.tableView 的委托和数据源)

- (void)setupFetchedResultsController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MainContent"];

    // I want to sort them by the "date" attribute input by [NSDate date].
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    // no predicate because we want ALL of the data in the "MainContent" entity.

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request                                                                
        managedObjectContext:self.mDoc.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
- (void)setMDoc:(UIManagedDocument *)mDoc {
    if (_mDoc != mDoc) _mDoc = mDoc;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.mDoc) {
        [[globalMDoc sharedDocumentHandler] performWithDocument:^(UIManagedDocument *coreDatabase) {
            self.mDoc = coreDatabase;
            [self setupFetchedResultsController];
        }];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Overview Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    MainContent *content = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = content.title;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [content date]];

    return cell;
}

【问题讨论】:

  • 使用单例,这样就不会被初始化两次。
  • 我很难将 UITableView 转换为单例,initWithFrame 函数丢失了...有解决方法吗?
  • 如何将 TableViewController 委托和数据源到这个单例?将其子类化为子类...?
  • 看看下面我的回答。

标签: ios uitableview ios5 uipopovercontroller reloaddata


【解决方案1】:

好的,像这个链接中的例子一样做一个单例:http://www.galloway.me.uk/tutorials/singleton-classes/

然后在你的 UITableView 所在的 UIViewController 的 Singleton 中创建一个 ivar。然后在 UITableView 视图的 VDL 中,执行类似

的操作

[Singleton sharedSingleton].theViewController = self;

然后在另一个视图中做类似的事情

    Singleton *singleton = [Singleton sharedSingleton];
    [singleton.mytableview reloadData];

但是,对于 (= self) 行,您可能希望更早地执行它,例如 applicationDidFinishLaunching 以便在您尝试访问时 theViewController 变量不为零。

【讨论】:

    【解决方案2】:

    我个人使用 NSNotifcations,我有几个异步发生的事件,我发回通知。

    也就是说,我所做的是在后台线程上下载图片,并在完成后重新加载表格。我通过向 tableviewclass 发送通知来做到这一点,然后 table view 类重新加载它自己的 tableview。

    ~丹

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 2023-03-31
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多