gpt4 book ai didi

ios - '-[__NSCFArray objectAtIndex :]: index (1) beyond bounds (1)' after item added to array

转载 作者:行者123 更新时间:2023-11-29 02:02:04 25 4
gpt4 key购买 nike

我有一个以两个空数组开始的 tableView(storageData 和 descripData - 因为用户尚未添加任何项目)。现在,当我的用户添加一个项目(标题、描述和照片)并刷新 tableView 时,出现错误:

'-[__NSCFArray objectAtIndex:]: 索引 (1) 超出范围 (1)'

也就是说,我不确定为什么。请参阅下面的代码;谢谢!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.storageData count] > 0 && self.descripData.count > 0)
{
return [self.storageData count];
}
else
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";

StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)

{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

if (self.storageData.count > 0 && self.descripData.count > 0) {

noitemsView.hidden = YES;

NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:@"%d",(long)indexPath.row]];


[[cell itemName] setText:(NSString *)[tmp objectForKey:@"title"]];

NSString *title = [tmp objectForKey:@"title"];
[[cell itemName] setText:title];

NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
[[cell itemDescrip] setText:[node objectForKey:@"body"]];
NSLog(@"%@", self.descripData);

NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:@"photo"];

[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];

NSLog(@"%@",secondLink);


}

else {
noitemsView.hidden = NO;
}
return cell;

}

最佳答案

仔细观察 self.descripData.count,因为在 numberOrRowsInSection 中您返回了 [self.storageData count]。但似乎 self.storageData.count > self.descripData.count,所以你试图让对象超出 self.descripData 范围。如此简单。

关于ios - '-[__NSCFArray objectAtIndex :]: index (1) beyond bounds (1)' after item added to array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239698/

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