繁体   English   中英

无法在每一行的cell.detailtextlabel中显示值

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

我有一个基于导航的UITableView应用程序。 这是我的应用程序的工作方式。

在rootViewController上,我有一系列问题,在nextViewController(这也是UITableView)上,我对这些问题有多项选择答案。 我希望用户点击这些问题,然后转到nextViewController选择答案,并在rootViewController的cell.detailTextLabel中的“问题”下显示该答案。

我能够得到答案,但是当我在rootViewController上显示它时,答案将显示在所有行上。 不确定如何完成此操作。

下面是我在rootViewController上的代码。 如果有人可以帮助我,我将不胜感激。 谢谢!

#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

上面的方法是配置每个单元。 当您将一个答案存储在应用程序委托中时,每个单元格都将得到相同的答案。

您需要做的是创建字典或以其他方式将数据存储在rootViewController中。 将答案放在其中,以便您存储所有答案,而不只是一个。

我将考虑制作一个对象数组,而不是制作一个字符串数组。 每个对象都有一个NSString *问题,一个NSArray *答案(其中将填充NSString答案),一个NSString *标题和一个NSString *字幕。

然后,当用户选择行之一时,您可以将该对象传递到下一个表格视图。 在下一个表格视图中,当一个人选择一个答案时,它将把该对象的字幕设置为该行的标签。

我意识到您可能希望这是一个简单的项目,但是我会为此使用Core Data。 这可能是过大的杀伤力,但它使事情变得容易得多,尤其是如果您想在以后使其变得更复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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