gpt4 book ai didi

ios - 通过 segue 时索引越界

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

我的 UITableView 中有 3 个自定义表格单元格。前两个单元格已设置位置,后跟随机数的单元格。

对于我的行数,我使用:

return [stepLabel count] +2;

对于我的表格单元格,我使用:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stepsCellIdentifier = @"StepsViewCell";
static NSString *descIdentfier = @"descViewCell";
static NSString *videoCellIdentfier = @"videoViewCell";

if( indexPath.row == 0 ) {
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:videoCellIdentfier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"videoViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.textLabel.text = [descLabel objectAtIndex:indexPath.row];

return cell;
}
else if( indexPath.row == 1 ) {
stepDescCell *cell = nil;
cell = (stepDescCell *)[tableView dequeueReusableCellWithIdentifier:descIdentfier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"descViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.descTextLabel.text = [descLabel objectAtIndex:indexPath.row - 1];

return cell;
}
else {
StepViewCell *cell = nil;
cell = (StepViewCell *)[tableView dequeueReusableCellWithIdentifier:stepsCellIdentifier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StepsViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

cell.stepTextLbl.text = [stepLabel objectAtIndex:(indexPath.row - 2)];
NSString *imageThumb = [thumbImg objectAtIndex:(indexPath.row - 2)];
[cell.thumbImage setImage:[UIImage imageNamed: imageThumb] forState:UIControlStateNormal];

return cell;
}
}

所有这一切都完美无缺,但是当我尝试通过 segue 将表中的字符串发送到另一个 UIViewController 时,出现以下错误:

index (-2 (or possibly larger)) beyond bounds (3)'

准备segue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"imageBigShow"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
imageBigViewController *destViewController = segue.destinationViewController;
NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];
destViewController.imageBigString = largeImgString;
}
}

我做错了什么?

更新

@implementation detailViewController
{
NSArray *thumbImg;
NSArray *stepLabel;
NSArray *descLabel;
NSArray *largeImg;
}

- (void)viewDidLoad

{
[super viewDidLoad];
// Find out the path of recipes.plist
NSDictionary *mapping = @{
@"How to wear a Kilt" : @"wearAKilt",
@"How to tie a cravat" : @"wearACravat",
@"How to wear a sporran" : @"wearASporran",
@"How to tie Ghillie Brogues" : @"wearTheShoes",
@"How to wear the accessories" : @"wearAccessories",
@"How to tie a cravat" : @"wearACravat",
@"How to Measure the Neck" : @"htmNeck",
@"How to Measure the chest" : @"htmChest",
@"How to Measure the waist" : @"htmWaist",
};

NSString *name = [mapping objectForKey:self.title];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];

// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
thumbImg = [dict objectForKey:@"thumbImg"];
stepLabel = [dict objectForKey:@"stepLabel"];
descLabel = [dict objectForKey:@"descLabel"];
largeImg = [dict objectForKey:@"largeImg"];

}

最佳答案

您有以下行:

NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];

如果用户点击第 1 行或第 2 行(第 0 行或第 1 行的索引路径),此代码将失败。

关于ios - 通过 segue 时索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532652/

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