简体   繁体   中英

Issue with UITableView datasource and delegate

I'm having a problem inserting cells into my TableView, and I suspect its because my viewController is not recognized as the delegate. I set up the viewController through storyboard and set its class to my custom ItemViewController class. My view controller is set up thus:

#import "ItemsViewController.h"
#import "DetailViewController.h"
#import "ItemCell.h"
#import "Item.h"
#import "ItemStore.h"

@implementation ItemsViewController

//designated initializer
-(instancetype) initWithStyle:(UITableViewStyle)style {
    return [self init];
}

-(void) viewDidLoad {
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
}

-(void) viewWillAppear:(BOOL)animated {
    //execute the super code in case there's work to be done by it
    [super viewWillAppear:YES];

    //reload the data to reflect any changes made either by a user or the program
    [self.tableView reloadData];

}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //create a detail view controller and initiate it with the selected item
    DetailViewController *dvc = [[DetailViewController alloc] init];
    NSArray *store = [[ItemStore sharedStore] allItems];
    Item *selectedItem = store[indexPath.row];
    dvc.item = selectedItem;

    //push the detail view controller onto the nav controller
    [self.navigationController pushViewController:dvc
                                     animated:YES];


}

-(void)  tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toIndexPath:(NSIndexPath *)destinationIndexPath {

    //move item from one index to another in the table
    [[ItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
                                     toIndex:destinationIndexPath.row];
}


-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {

    //number of rows in the table. will adjust with every new item added
    return [[[ItemStore sharedStore] allItems] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //set up the cell
    NSString static *cellIdentifier = @"itemCell";
    ItemCell *newCell = [tableView    dequeueReusableCellWithIdentifier:cellIdentifier];

    if (newCell == nil) {
        newCell = [[ItemCell alloc]   initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    //retrieve the item at the appropriate index
    NSArray *items = [[ItemStore sharedStore] allItems];
    Item *newItem = items[indexPath.row];

    newCell.nameLabel.text = newItem.itemName;
    newCell.serialLabel.text = newItem.serialNumber;
    newCell.valueLabel.text = [NSString stringWithFormat:@"%d",    newItem.value];

    return newCell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //create an empty item object
    Item *newItem = [[ItemStore sharedStore] createItem];
    NSArray *itemStore = [[ItemStore sharedStore] allItems];

    //insert a row with the appropriate path
    NSIndexPath *path = [NSIndexPath indexPathForRow: [itemStore count]
                                       inSection:0];

    [self.tableView insertRowsAtIndexPaths:@[path]
                      withRowAnimation:UITableViewRowAnimationTop];
    DetailViewController *dvc = segue.destinationViewController;
    dvc.item = newItem;
}

and my storyboard thus: 在此处输入图片说明

My intention is that when the user clicks "Add Item", a new cell is created in the tableView along with an empty Item object that I would pass to the (DetailViewController) I'm segue-ing to.

When I try to run this code I get the error: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 0, but there are only 1 rows in section 0 after the update'" From what I've read online this is because the datasource/delegate isn't returning the correct number of rows in "numberOfRowsInSection". The sources of my items (itemStore, item) are both fine as attested to by the debugger.

Does anyone have any input on this? am I setting the delegate/datasource for my tableView wrong?

EDIT: provided storyboard screenshot and explained the "Add Item" interface

I think, you are trying to create wrong NSIndexPath . Try this:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        //create an empty item object
        Item *newItem = [[ItemStore sharedStore] createItem];
        NSArray *itemStore = [[ItemStore sharedStore] allItems];

        //insert a row with the appropriate path
        NSIndexPath *path = [NSIndexPath indexPathForRow: ([itemStore count]-1)
                                           inSection:0];

        [self.tableView insertRowsAtIndexPaths:@[path]
                          withRowAnimation:UITableViewRowAnimationTop];
        DetailViewController *dvc = segue.destinationViewController;
        dvc.item = newItem;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM