ios - UITableView 数据源和委托(delegate)的问题

标签 ios uitableview

我在将单元格插入 TableView 时遇到问题,我怀疑这是因为我的 viewController 未被识别为委托(delegate)。我通过 Storyboard设置了 viewController,并将其类设置为我的自定义 ItemViewController 类。我的 View Controller 是这样设置的:

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

我的 Storyboard是这样的: enter image description here

我的意图是,当用户单击“添加项目”时,将在 tableView 中创建一个新单元格以及一个空的 Item 对象,我会将其传递给我正在继续访问的 (DetailViewController)。

当我尝试运行此代码时出现错误:“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因:‘尝试将第 1 行插入第 0 节,但第 0 节中只有 1 行更新后'" 根据我在网上阅读的内容,这是因为数据源/委托(delegate)未在“numberOfRowsInSection”中返回正确的行数。我的项目(itemStore、item)的来源都很好,调试器证明了这一点。

有人对此有任何意见吗?我是否为我的 tableView 设置了委托(delegate)/数据源错误?

编辑:提供 Storyboard截图并解释“添加项目”界面

最佳答案

我认为,您正在尝试创建错误的 NSIndexPath。 试试这个:

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

关于ios - UITableView 数据源和委托(delegate)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457238/

相关文章:

iphone - UICollectionViewCell 抖动

iphone - 如何在iPhone的速度计中旋转图像

iphone - 如何提取安装了您的 iOS 应用程序的 Facebook 好友

swift - UITableViewCell 异步加载图像问题 - Swift

ios - 当我点击返回键时键盘没有退出 (xcode)

iphone - UIScrolview 优先垂直和水平滚动

ios - UITableView - 在滚动时它会丢失与选定单元格关联的属性。如何保存这些属性? - swift

ios - cellForRowAtIndexPath : method in UITableViewDelegate

iphone - 什么时候需要在转换 iOS 后重置 view.frame 属性

c# - 如何使数字键盘出现在 Xamarin.iOS 中的 View Controller 条目上?