Ios 通过segue时索引超出范围
我的UITableView中有3个自定义表格单元格。前两个单元格设置了位置,后面是随机数目的单元格 对于我的行计数,我使用:Ios 通过segue时索引超出范围,ios,uistoryboardsegue,nsindexpath,Ios,Uistoryboardsegue,Nsindexpath,我的UITableView中有3个自定义表格单元格。前两个单元格设置了位置,后面是随机数目的单元格 对于我的行计数,我使用: return [stepLabel count] +2; 对于我的TableCell,我使用: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *stepsCell
return [stepLabel count] +2;
对于我的TableCell,我使用:
- (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;
}
}
所有这一切都非常有效,但是,当我试图通过一个段将一个字符串从表发送到另一个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行的索引路径),此代码将失败。我在没有-2的情况下尝试了此代码,但它只传递相同的图像,我如何解决此问题?您没有提供正确的信息来回答此问题。我需要显示什么?full cellForRowAtIndexPath方法?从何处准备segue?如果它位于
didSelectRowAtIndexPath
中,请向我们显示完整的didSelectRowAtIndexPath
code.Hi@AkshitZaveri。我没有DIDSELECTROWARINDEXPATH。您在哪里编写代码将NSString
传递给其他UIViewController
?我在上面的prepareForSeague方法中编写了这段代码,为什么要使用indexPath.row-2
?有什么理由这么做吗?