我有两个视图,第一个(视图A)是一个表视图。每行都有一个公开按钮,当我点击按钮时,我会显示第二个视图(视图B),这也是一个表视图。当我点击一行时,我想将数据传递给视图A并将其显示为detailTextLabel
。
#import <UIKit/UIKit.h>
@interface A : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSDictionary *listData;
NSArray *keys;
}
@property (nonatomic, retain) NSDictionary *listData;
@property (nonatomic, retain) NSArray *keys;
@end
#import "A.h"
@implementation A
@synthesize listData;
@synthesize keys;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[listData release];
[keys release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Criteres" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.listData = dict;
[dict release];
NSArray *array = [[listData allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
self.keys = array;
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.listData = nil;
self.keys = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [listData objectForKey:key];
return [nameSection count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
return key;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [listData objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
if (section == 0) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
if (row == 2)
cell.detailTextLabel.text = @"En m2";
if (row == 1)
cell.detailTextLabel.text = @"En €";
}
if (section == 1) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
}
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = 2;
return row;
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];
if ([ownerCell.textLabel.text isEqualToString:@"Ecole"]) {
B *prixview = [[B alloc]init];
[self.navigationController pushViewController:prixview animated:YES];
prixview.title = @"prix";
}
if ([ownerCell.textLabel.text isEqualToString:@"Crèche"]) {
B *prixview = [[B alloc]init];
[self.navigationController pushViewController:prixview animated:YES];
prixview.title = @"Prix";
}
}
@end
#import <UIKit/UIKit.h>
@interface B : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *typetab;
NSInteger radioSelectionTypeBien;
NSString *radioSelectionTypeBienString;
}
@property (nonatomic, retain) NSArray *typetab;
@property (nonatomic, retain) NSString *radioSelectionTypeBienString;
@end
#import "B.h"
@implementation B
@synthesize typetab;
@synthesize radioSelectionTypeBienString;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[self.radioSelectionTypeBienString release];
[self.typetab release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"Critères"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(backButtonActionTypeBien:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
radioSelectionTypeBien = -1;
NSArray *type = [[NSArray alloc]initWithObjects:@"Appartement",@"Maison",@"Commerce", nil ];
self.typetab = type;
[type release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)backButtonActionTypeBien: (id)sender
{
[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}
- (void)viewDidUnload
{
self.radioSelectionTypeBienString = nil;
self.typetab = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.typetab count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [typetab objectAtIndex:indexPath.row];
if (indexPath.row == radioSelectionTypeBien){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
radioSelectionTypeBien = indexPath.row;
radioSelectionTypeBienString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[tableView reloadData];
}
@end
答案 0 :(得分:1)
通常我在控制器B
中编写一个方法,如:
-(voi)initVCwithString:(NSString*)_string andSomeObject:(NSObject *)_obj;
(所以控制器B必须有2个ivar,一个字符串和一个对象,你必须在这个方法中设置)
然后在您的didSelectRowAtIndexPath
:来自控制器A
...
B _vcB=//alloc..init..etc
[_vcB initVCwithString:yourCell.textLabel.text andSomeObject:someObj];
[self.navigationController pushViewController:_vcB animated:YES];
[_vcB release];
然后在cellForRowAtIndexPath
B
设置cell.detailTextLabel.text=yourStringIvar;
希望这有帮助。
答案 1 :(得分:0)
基本上有两种可能性似乎是合适的。您可以编写一个自定义委托方法,将值传递给A控制器,或者您可以通过通知中心发出通知,通知中心可以通过捕获通过选择单元格中的单元格触发的通知将值分配给A控制器。控制器B.