Hello,
I'm having a bit of a problem here. I have a navigation controller with only two view controllers in its stack. Typically, the view controllers are table views (the two view controllers conforms to table view datasource and delegate protocols).
The root view controller/TableView has an image in each of its cells (cell.imageView.image) and when I select a row/cell it pushes to the second view controller.
The second view controller/TableView has its cells having buttons as an accessory view (cell.accessoryView = button)
What I actually trying to achieve is: I want to change the cell image of the selected row of the root table view if I tapped a button or more in the child table view and popped back to the root table view and of course not to change it if I did not do any changes in the child table view.
I have been working on that for a long time I tried using delegates and protocols between the controllers but either I'm doing something wrong or this is not the right way to do it???
Another issue, is that, in the child table view if I tapped a button(s) it change its state from normal to highlighted but when I pop back to the root table view then back again to the child table view it restores its state again to normal. What I want it to behave is to keep its state whether it was highlighted or normal but it does not seem to work for me.
Any help, suggestion, or direction would be really greatly appreciated. Thanks a lot in advance.
Ok, I'm posting some of the source code hope this helps:
// RootViewController.h
@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, FirstViewControllerDelegate> {
. . .
}
. . .
@end
// RootViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
NSString *rowString = [self.tableData objectAtIndex:row];
cell.textLabel.text = rowString;
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.numberOfLines = 0;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.minimumFontSize = 14;
cell.imageView.image = [UIImage imageNamed:@"checkbox_no.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.imageView.image = [UIImage imageNamed:@"checkbox_yes.png"];
// Navigation logic may go here. Create and push another view controller.
ChildViewController *childViewController = [[ChildViewController alloc] init];
Manager *manager = [Manager sharedManager];
manager.filterString = [self.tableData objectAtIndex:[indexPath row]];
childViewController.tableData = [manager.sourcesDict objectForKey:manager.filterString];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:childViewController animated:YES];
[childViewController release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.lastIndexPath = indexPath;
//NSLog(@"lastIndexPath: %d", [self.lastIndexPath row]);
}
- (void)childViewControllerDidFinish {
UITableViewCell *cell = [self.table cellForRowAtIndexPath:self.lastIndexPath];
cell.imageView.image = [UIImage imageNamed:@"checkbox_no.png"];
}
// ChildViewController.h
@protocol ChildViewControllerDelegate;
@interface ChildViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
. . .
id <FirstViewControllerDelegate> delegate;
}
. . .
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
- (void)subButtonTapped:(id)sender;
- (IBAction)backToSourcesButtonTapped:(id)sender;
@end
@protocol ChildViewControllerDelegate
- (void) childViewControllerDidFinish;
@end
// ChildViewController.m
. . .
- (void)subButtonTapped:(id)sender {
checkmarkFlag = YES;
.....
}
- (IBAction)backToSourcesButtonTapped:(id)sender {
if (!checkmarkFlag)
[self.delegate childViewControllerDidFinish];
[self.navigationController popViewControllerAnimated:YES];
}
Thanks in advance ...