![](/img/trans.png)
[英]Change UITableView cell.detailTextLabel colour on highlight
[英]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.