多个表的CheckMark索引问题

时间:2013-06-09 00:05:23

标签: ios objective-c

这是我在Storyboards中的项目结构:

导航视图控制器------>主视图控制器------> 2个子视图控制器

两个子视图控制器中的每一个都有一个单独的表和视图控制器。

下面的代码工作得非常好,但我面临的问题是我可以说我在这个表中选择索引2(将出现一个复选标记图像):它将把我带到主ViewController,但是然后从主ViewController如果我点击一个按钮导航到另一个子视图,则该子视图打开时带有一个已经在索引2处的复选标记图像(反之亦然)。

以下是我的一个ChildViewControllers的代码:

#import "MainVC"
#import "ChildVC"
@interface ChildVC ()
@end

@implementation ChildVC

NSIndexPath *lastIndexPath;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    tableArr =[[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"Other",nil];
    self.navigationItem.leftBarButtonItem=nil;
    self.navigationItem.hidesBackButton=YES;
    self.navigationItem.title = @"Select Language";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
    [cell.contentView addSubview:label];
    label.text = [tableArr objectAtIndex:indexPath.row];
    label.tag = 1;
    label.textColor = [UIColor colorWithRed:47.0f/255.0f green:111.0f/255.0f blue:182.0f/255.0f alpha:1.0f];
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
    cell.accessoryView = checkmark;
    return cell;
}

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath 
{    
    lastIndexPath = indexPath;
    [tableView reloadData];

    // Here I am sending the Value of Selected Row to Main View Controller and storing in typelabel.text
    UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *lbltemp = (UILabel *)[cell1 viewWithTag:1];
    _parent.typelabel.text = lbltemp.text;

    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 9;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell      forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ([lastIndexPath isEqual:indexPath]) {
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone-checkMark.png"]];
    } else {
        cell.accessoryView = nil;
    }
} 

@end

0 个答案:

没有答案