我在viewController中有一个带有数组的tableView(3个labelTitles到3个不同的detailViews)。我的问题是在prepareForSegue方法中,我真的不知道如何调用detailViews。我使用正确的segue标识符名称。
“主” 的.m:
#import "GuideTableViewController.h"
#import "GuideDetailTableViewController.h"
#import "GuideDetailTableViewController2.h"
#import "GuideDetailTableViewController3.h"
#import <QuartzCore/QuartzCore.h>
#import "customImageCell.h"
@interface GuideTableViewController (){
NSMutableData *weatherResponseData;
NSArray *titleLabels;
NSArray *imagesLeft;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *imgHeader;
@property (weak, nonatomic) IBOutlet UIImageView *ImgTitle;
@property (weak, nonatomic) IBOutlet UIImageView *ImgWeather;
@property (weak, nonatomic) IBOutlet UIButton *btnMap;
@property (weak, nonatomic) IBOutlet UILabel *LabelWeather;
@property (weak, nonatomic) IBOutlet UILabel *LabelWeather2;
@end
@implementation GuideTableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//Weather method
- (void) loadWeather{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://api.wunderground.com/api/3919480da5014c98/conditions/q/BR/Sao_Sebastiao.json"]];
NSURLConnection *theConnection=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if(theConnection){
weatherResponseData = [[NSMutableData alloc] init];
} else {
NSLog(@"failed");
}
}
//Delegates for WeatherData
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[weatherResponseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[weatherResponseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSString *msg = [NSString stringWithFormat:@"Failed: %@", [error description]];
NSLog(@"%@",msg);
}
//All the data was loaded, let's see what we've got...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:weatherResponseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *results = [res objectForKey:@"current_observation"];
NSString *cur = [results valueForKey:@"weather"];
NSString *tmp = [results valueForKey:@"temperature_string"];
NSString *wind = [results valueForKey:@"wind_string"];
NSLog(@"Current conditions: %@, %@º, %@", cur, tmp, wind);
self.LabelWeather.text = cur;
self.LabelWeather2.text = tmp;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadWeather];
titleLabels = [NSArray arrayWithObjects:@"Where to stay",@"Where to eat",@"What to do",nil];
imagesLeft = [NSArray arrayWithObjects:@"btn_Stay.png", @"btn_Eat.png", @"btn_Todo.png", nil];
//set background
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
//rounded corners
[self.tableView.layer setCornerRadius:9.0];
[self.ImgWeather.layer setCornerRadius:9.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return titleLabels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
customImageCell *cell = (customImageCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *cellLabel = [titleLabels objectAtIndex:indexPath.row];
cell.titleLabel.text = cellLabel;
NSString *cellImage = [imagesLeft objectAtIndex:indexPath.row];
UIImage *cellIcon = [UIImage imageNamed:cellImage];
cell.imageLeft.image = cellIcon;
return cell;
}
//To detailTableViewController
- (void)tableView:(UITableView *)tableView didselectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0){
[self performSegueWithIdentifier:@"stay" sender:self];
}else if(indexPath.row ==1 ){
[self performSegueWithIdentifier:@"eat" sender:self];
}else{
[self performSegueWithIdentifier:@"todo" sender:self];
}
}
- (void) viewWillAppear:(BOOL)animated{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
[cell setSelected:NO];
//Hide navbar
[self.navigationController setNavigationBarHidden:YES];
}
//Show navbar in detailView
-(void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO];
}
@end
答案 0 :(得分:0)
改变didselectRow
- (void)tableView:(UITableView *)tableView didselectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0){
[self performSegueWithIdentifier:@"stay" sender:self];
}else if(indexPath.row ==1 ){
[self performSegueWithIdentifier:@"eat" sender:self];
}else{
[self performSegueWithIdentifier:@"todo" sender:self];
}
}