我在将单元格插入到我的TableView时遇到问题,我怀疑它是因为我的viewController未被识别为委托。我通过storyboard设置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)我的segue - 到。
当我尝试运行此代码时,我收到错误:"由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'尝试将第1行插入到部分0,但更新后第0部分只有1行'" 从我在线阅读的内容这是因为数据源/代理人没有返回正确的数字" numberOfRowsInSection"中的行数。我的项目的来源(itemStore,item)都很好,调试器证明了这一点。
有人对此有任何意见吗?我为tableView设置委托/数据源是错误的吗?
编辑:提供故事板截图并解释"添加项目"接口
答案 0 :(得分:1)
我认为,您正在尝试创建错误的 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;
}