-1

Here is my problem:

By pushing a button on the DetailViewController I want to receive the data of my next row.

Here is what I tried so far:

-(IBAction)next:(id)sender {
question.text = [questions objectAtIndex:indexPath.row+1];
}

But now it takes the data that is stored in row 1. But I want to retrieve the data from the next row in line.

questionis the name of my label I want the text to be displayed in.
questionsis the name of my NSMutableArray o store my data in.

Here is the complete Code:

//
//  NINDetailViewController.m
//  NIN
//
//  Created by Hegg Roger on 27.06.13.
//  Copyright (c) 2013 Hegg. All rights reserved.
//

#import "NINDetailViewController.h"
#import "NIN.h"

@interface NINDetailViewController ()

@end

@implementation NINDetailViewController

@synthesize transfer;
@synthesize content;
@synthesize questions;
@synthesize questions1;
@synthesize answers;
@synthesize question;
@synthesize questionTableView;
@synthesize indexPath;


/*
-(IBAction)next:(id)sender {
question.text = [questions objectAtIndex:indexPath.row+1];
}      


-(IBAction)prev:(id)sender {

}
*/

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{

questions = [[NSMutableArray alloc]initWithObjects:
              @"1. Wie lautet die richtige Reihenfolge beim Arbeiten an elektrischen
Installationen (Arbeitssicherheit)?",
             @"2. Was versteht man unter dem Begriff UVG?",
             @"3. Was ist die wichtigste Bestimmung des UVGs?",nil];



questions1 = [[NSMutableArray alloc]initWithObjects:
             @"1. Was bedeuten die Abkürzungen NIV / NIN / StV?",nil];


[super viewDidLoad];




content.text = transfer;
 //  question.text = [questions objectAtIndex:indexPath.row];


}



- (NSInteger)tableView : (UITableView *)tableView numberOfRowsInSection:(NSInteger)section       
{


return [questions count];
 }

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    
 *)indexPath
{
static NSString *simpleTableIdentifier = @"questionCell";

UITableViewCell *cell = [questionTableView 
dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:simpleTableIdentifier];
}

if ([content.text isEqualToString:@"Unfallverhütung"]) {
    question.text = [questions objectAtIndex:indexPath.row];
}
else
    if ([content.text isEqualToString:@"Grundlagen / Begriffe / Kennzeichnungen"]) {
        question.text = [questions1 objectAtIndex:indexPath.row];
    }



return cell;
}



- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

@TBlue: I got this Error with your suggestion: enter image description here

4

3 回答 3

0

尝试类似的东西

@interface ViewController () {
    NSInteger selectedindex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    selectedindex = -1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedindex = indexPath.row;
}

- (IBAction)next:(id)sender {
    if (questions.count == 0 || selectedindex < 0) {
        return;
    }
    question.text = [questions objectAtIndex:selectedindex + 1];
}
于 2013-06-28T21:47:03.197 回答
0

除非 indexPath 是您在某处定义的全局变量,否则这里有一个非常明显的问题。也就是说, indexPath 不在您在上面发布的方法中访问它的范围内......除非您未能在该方法中发布整个代码......

于 2013-06-28T19:49:06.120 回答
0

你的阵列在哪里举行?变量呢?

一些代码在这里真的会有所帮助。听起来您的 indexPath 没有在您使用它的地方定义,这意味着您可能会添加 nil + 1,这可能是您的第 1 行问题的根源。

发布一些代码,以便我们提供更多帮助!

于 2013-06-28T20:15:18.953 回答