简体   繁体   中英

Not able to display values in cell.detailtextlabel for each row

I have a navigation based UITableView app. Here is how my app works.

On the rootViewController I have set of questions and on nextViewController(this is also UITableView) I have multiple choice answers for these questions. I want user to tap on these questions and go to nextViewController select the answer and display that answer underneath the Question in cell.detailTextLabel of the rootViewController.

I am able to get the answer, but when I display it on the rootViewController, answer is displayed on all the rows. Not sure how to accomplish this.

Below is my code on rootViewController. I will appreciate If anyone can please help me on this. Thanks!

#import "RootViewController.h"
#import "NextViewController.h"
#import "xxxAppDelegate.h"

@implementation RootViewController
@synthesize questions;
@synthesize questAns;
@synthesize aNote;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"xxx";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Submit" style:UIBarButtonItemStyleBordered target:self action:@selector(SubmitBtn:)] autorelease];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(ClearBtn:)] autorelease];
    questions = [[NSArray alloc] initWithObjects:@"Question 1", @"Question 2", @"Question 3", nil];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
#pragma mark -
#pragma mark Table view data source

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [questions count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    cell.textLabel.text = [questions objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    xxxAppDelegate *mainDelegate = (xxxAppDelegate *)[[UIApplication sharedApplication]delegate];
    cell.detailTextLabel.text = mainDelegate.MainLocLbl; 

    NSLog(@"Indexpath.row on root %d", indexPath.row);
    NSLog(@"detail text %@", mainDelegate.MainLocLbl);

    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     NextViewController *detailViewController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    //questAns = [questions objectAtIndex:indexPath.row];
    //detailViewController.answers = questAns;
    xxxAppDelegate *mainDelegate = (xxxAppDelegate *)[[UIApplication sharedApplication]delegate];

    detailViewController.aNote = mainDelegate.MainLocLbl;
    NSLog(@"dtl ans %@", detailViewController.aNote);
    [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];

}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

The above method is configuring every cell. When you store one answer in your app delegate, every cell is getting the same answer.

What you need to do is create dictionary or some other way of storing the data in your rootViewController. put the answer in there with so that you are storing all the answers, instead of just one.

Instead of making an array of Strings, I would think about making an array of objects. Each object would have an NSString *question, an NSArray *answers which would be populated with NSString answers, an NSString *title and an NSString *subtitle.

Then when a user selects one of the rows, you would pass that object to the next tableview. In the next tableview when a person selects one of the answers it will set the subtitle of that object to the label of that row.

I realize that you may want this to be a simple project but I would use Core Data for this. It may be overkill but it makes things so much easier, especially if you want to make it more complex later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM