-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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hEgg
  • 29
  • 7

3 Answers3

0

unless indexPath is a global variable you've defined somewhere, you have a pretty obvious problem here. Namely, indexPath is not in scope where you are accessing it in your method you posted above... unless you failed to post the entire code in the method...

HackyStack
  • 4,887
  • 3
  • 22
  • 28
0

Where is your array held? What about variables?

Some code would really help here. It sounds like your indexPath isn't defined where you're using this, which means you're likely adding nil + 1, which may be the source of your row 1 problem.

Post up some code so we can help more!

Rob
  • 1,045
  • 13
  • 28
0

Try something like

@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];
}
El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • THX for the effort but unfortunatly i get errors: (see the Pic) – hEgg Jun 29 '13 at 08:09
  • I suggest you take a good look at the very first three lines of code that I've posted. – El Tomato Jun 30 '13 at 00:32
  • Thanks again but the complete code that i posted dosn`t contain your solution. I`v tried it, got the errors, took the screenshot and erased it again. I forgot to mention that i`m using Storyboard and ARC. – hEgg Jun 30 '13 at 11:16
  • You are getting an error because you are not implementing selectedindex as a class-wide variable. You don't seem to listen. So that's all from me. – El Tomato Jun 30 '13 at 17:33