ios - UITableViewController 通过方法添加项目

标签 ios objective-c uitableview uisplitviewcontroller

我正在使用 UISplitviewController 并尝试将项目添加到 TableView 。

现在我有两种方法

  1. 创建一个按钮并添加它:

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    

此代码在单击按钮时运行:

- (void)insertNewObject:(id)sender {
    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

并向 TableView 添加一个项目:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

这种方式可行。

这就是我想要做的:

- (void)GetRequest
{

    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

但这不会更新 TableView 。我添加了一个断点,当我使用我添加的按钮时,它进入 TableView ,使用它没有的方法,我通过 [MasterController GetRequest];

调用该方法

我做错了什么?

我正在从另一个 Controller 调用 GetRequest。

这就是 MasterController 的定义方式:

@interface DetailController ()
{

    MasterController *MasterController;

}

DetailController.m:

#import "MasterController.h"

@interface DetailController ()
{
     MasterController *MasterController;
}

@end

@implementation DetailController

-(void)viewDidLoad {
     MasterController = [[MasterController alloc]init];
}

[MasterController GetRequest];

MasterController.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (void)GetRequest
{

    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:0];
    [self.tableView reloadData];

}

最佳答案

正如我所怀疑的,通过这一行 MasterController = [[MasterController alloc]init],您正在创建一个与您在屏幕上看到的实例无关的新实例。您需要获得对 Split View Controller 中已有的主 Controller 的引用。从详细 View Controller 中,您可以像这样获取,

MasterController = self.splitViewController.viewControllers.firstObject;

split view controller 有一个 viewControllers 属性,index 0 的是 master,index 1 的是 detail。顺便说一句,您应该以较低级别的字母开始您的 ivars 和方法名称。

关于ios - UITableViewController 通过方法添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29971827/

相关文章:

ios - 拾取和弹出力触摸手势有时不起作用

iphone - 我有一个应用程序需要使用自定义 URL 方案打开另一个应用程序。我能以某种方式找出我需要打开的应用程序的版本号吗?

ios - 以编程方式创建的 TableView 的延迟加载单元格图像

ios - 尝试重新加载部分时断言失败错误

ios - Obj-c - 返回原始状态时延迟动画?

iphone - 使用自定义 segue 动画将一个 View 替换为另一个 View ?

iphone - 如何为一组嵌套的 UIView animateWithDuration 调用调用单个完成 block ?

ios - 为什么我在 iTunes Connect 中没有看到自动续订订阅

ios - 我如何从 native swift 代码中使用 native RCNAsyncStorage 模块

swift - 如何快速从 UITableView 中删除部分中的页眉 View 和页脚 View ?