簡體   English   中英

UITableView數據源和委托的問題

[英]Issue with UITableView datasource and delegate

我在將單元格插入到TableView中時遇到問題,我懷疑是因為我的viewController無法識別為委托。 我通過情節提要板設置了viewController,並將其類設置為我的自定義ItemViewController類。 我的視圖控制器設置如下:

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

因此我的情節提要: 在此處輸入圖片說明

我的意圖是,當用戶單擊“添加項目”時,將在tableView中創建一個新單元格,同時創建一個空的Item對象,該對象將傳遞給我要定位的(DetailViewController)。

當我嘗試運行此代碼時,出現錯誤: “由於未捕獲的異常'NSInternalInconsistencyException'而終止應用程序,原因:'試圖將第1行插入第0節,但更新后第0節中只有1行”我在線閱讀的內容是因為數據源/委托未在“ numberOfRowsInSection”中返回正確的行數。 我的項目(itemStore,item)的來源都很好,調試器對此進行了證明。

有人對此有任何意見嗎? 我為我的tableView設置了委托/數據源嗎?

編輯:提供情節提要截圖並解釋了“添加項目”界面

我認為您正在嘗試創建錯誤的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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM