gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:05:21 24 4
gpt4 key购买 nike

我在将单元格插入 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com