Reputation: 690
I'm using a table view to display an array of data and I would like to pass the value to update Labels and image view in the detail view controller. I actually created a custom cell and I loaded it in the Table view and on selecting a row i pushed a new custom view controller with labels and ImageView. I would like to use the values of selected cell to be displayed in the labels in custom view controller. The code compiles but the only thing is that the Labels and image view in the detail view doesn't get updated. I just don't know where I'm going wrong…I'm pretty much stuck with for quite sometime. below is the code in my app…
code in table view…
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.LocationLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.TimeLabel.text = [time objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
TrafficTableViewViewController.m
@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
in the above code TrafficDetailViewController is a custom view controller I've created and it contains labels and UIImage. Please let me know where I'm going wrong...
Upvotes: 1
Views: 1205
Reputation: 3990
Try this code, may it will solve your issue. the problem is that you are setting the properties after pushing the controller which is wrong. try the below code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.trafficImage = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
EDIT
.h File in .m File
Add the synthesizer if you are not adding yet.
@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
Also Edit one more thing when you are trying to push Your TrafficDetailViewController
then please assign image to trafficImage.image not to trafficImage and string to LocationLabel.text because trafficImage is an ImageView not an Image and LocationLable is a label not string.So replace your line with following code
trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
POST EDIT
in .h file
@interface TrafficDetailViewViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *trafficImage;
@property (strong, nonatomic) IBOutlet UILabel *LocationLable;
//Add these two more properties in TrafficDetailViewController
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *locationString;
@end
in .m File:
@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
@synthesize image = _image;
@synthesize locationString = _locationString;
in viewDidLoad method:
- (void) viewDidLoad
{
self.trafficImage.image = _image;
self.LocationLabel.text = _locationString;
}
and then in Previous viewController where you have written the push code for TrafficDetailviewController replace the following code with yours
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.locationString = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
The reason behind this is like your ViewController is not loaded before pushing and we are trying to set imageView.image and Lable.text which I think is causing the issue. Please try this and let me know is it working now?
Enjoy coding!!
Upvotes: 2
Reputation: 104082
Your problem may be in the way you instantiate TrafficDetailViewViewController, using alloc init. This will create a controller with an empty view, and is probably not the one you want to push on screen. If you created this controller's view in a xib, then you should be using initWithNibName:bundle: instead of init. If you created it in a storyboard, then you should use [self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"]. This line could be a problem too, I can't tell what you're trying to do here:
trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
Is LocationLable a label, or is it a string? To populate a label, you should pass the string to the next controller, and have that controller set the text of its own label.
Upvotes: 1
Reputation: 131426
Change the order of your code that creates and pushes the trailsController. Create it, set the properties, and THEN push it.
That may or may not be the cause of your problem. Next you need to debug the trailsController. What do you do with the properties trafficImage and locationTable? Do you have custom setters, or do you use them in your viewDidLoad or viewWillAppear methods? Post the relevant code from those methods in the other view controller.
Upvotes: 1