UITableViewController通过方法添加项目

时间:2015-04-30 15:29:24

标签: ios objective-c uitableview uisplitviewcontroller

我正在使用UISplitviewController,我正在尝试将项添加到表视图中。

现在我有两种方式

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

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    
  2. 并且在单击按钮时运行此代码:

    - (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];
    }
    

    并在表格视图中添加一个项目:

    - (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];
    
    }
    

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

    我做错了什么?

    我从另一个控制器调用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];
    
    }
    

2 个答案:

答案 0 :(得分:1)

UITableViewController并没有真正起作用。 如果你继承它,你需要实现几个方法来告诉表它有什么数据 -

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.objects.count;
}

- (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;
}

要添加新行,您只需将对象添加到self.objects并调用[self.tableView reloadData];

基本上是 -

- (void)GetRequest
{

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

答案 1 :(得分:1)

我怀疑,使用此行MasterController = [[MasterController alloc]init],您创建的新实例与您在屏幕上看到的实例无关。您需要获得对拆分视图控制器中已有的主控制器的引用。从详细视图控制器,您可以这样做,

MasterController = self.splitViewController.viewControllers.firstObject;

拆分视图控制器具有viewControllers属性,索引0处的属性为master,索引1处的属性为detail。顺便说一句,你应该用较低的类别字母开始你的ivars和方法名称。