[英]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. 我在将单元格插入到TableView中时遇到问题,我怀疑是因为我的viewController无法识别为委托。 I set up the viewController through storyboard and set its class to my custom ItemViewController class. 我通过情节提要板设置了viewController,并将其类设置为我的自定义ItemViewController类。 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. 我的意图是,当用户单击“添加项目”时,将在tableView中创建一个新单元格,同时创建一个空的Item对象,该对象将传递给我要定位的(DetailViewController)。
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". 当我尝试运行此代码时,出现错误: “由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'试图将第1行插入第0节,但更新后第0节中只有1行”我在线阅读的内容是因为数据源/委托未在“ numberOfRowsInSection”中返回正确的行数。 The sources of my items (itemStore, item) are both fine as attested to by the debugger. 我的项目(itemStore,item)的来源都很好,调试器对此进行了证明。
Does anyone have any input on this? 有人对此有任何意见吗? am I setting the delegate/datasource for my tableView wrong? 我为我的tableView设置了委托/数据源吗?
EDIT: provided storyboard screenshot and explained the "Add Item" interface 编辑:提供情节提要截图并解释了“添加项目”界面
I think, you are trying to create wrong NSIndexPath . 我认为您正在尝试创建错误的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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.