无法保持选择表行

时间:2013-04-01 04:43:46

标签: ios objective-c uitableview

在我的应用中,我使用了splitView。有3个不同的表。

当我从第一张表中选择任何一行时,详细视图中的显示和&在左侧菜单中保持选中行。但是,当我从第2或第3表中选择任何一行时,该行仅用蓝色和&快速消失选定的蓝色(即它不会保持突出显示)。

帮我解决这个问题。

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier=@"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if(tableView == tbl_class)
    {

         btnEdit1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnEdit1 setFrame:CGRectMake(220, 15, 20, 20)];
        [btnEdit1 addTarget:self action:@selector(btnEditPressed:) forControlEvents:UIControlEventTouchUpInside];

        [btnEdit1 setImage:[UIImage imageNamed:@"edit-3-black.png"]                   forState:UIControlStateNormal];

        UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnDelete setFrame:CGRectMake(260, 15, 20, 20)];
        [btnDelete addTarget:self action:@selector(btnDelete:) forControlEvents:UIControlEventTouchUpInside];

        [btnDelete setImage:[UIImage imageNamed:@"recycle-bin2.png"]                   forState:UIControlStateNormal];

        [btnEdit1 setTag:indexPath.row];
        [btnDelete setTag:indexPath.row];

        cell.textLabel.text=[NSString stringWithFormat:@"%@",[[classNames objectAtIndex:indexPath.row]valueForKey:@"class_name"]];

        if(! isEditing)
        {
            [cell.contentView addSubview:btnEdit1];
            [cell.contentView addSubview:btnDelete];
        }



     }
     if(tableView==tbl_assessment)
    {
        if (classNames.count > 0)
        {
            [btnAddAssesst setHidden:NO];
        }
        else
        {
            [btnAddAssesst setHidden:YES];
        }



       cell.textLabel.text=[NSString stringWithFormat:@"%@",[[assessment_list objectAtIndex:indexPath.row]valueForKey:@"assessment_name"]];

    }

    if (tableView==tbl3)
    {

        cell.textLabel.text=[arr_result objectAtIndex:indexPath.row];

    }

    cell.textLabel.font=[UIFont fontWithName:@"Trebuchet MS" size:17.0f];


    cell.textLabel.textColor=[UIColor grayColor];

    return cell;
}


- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    /*
     When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
     */
    //NSUInteger row = indexPath.row;

    if (aTableView==tbl_class)
    {
          [self.appDelegate.splitViewController viewWillDisappear:YES];
    NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
    [viewControllerArray removeLastObject];


        self.secondDetailViewController=[[[Class_Vice_ViewController alloc]init]autorelease];
        [viewControllerArray addObject:self.secondDetailViewController];
        self.appDelegate.splitViewController.delegate = self.secondDetailViewController;

        [[NSUserDefaults standardUserDefaults]setInteger:[[[classNames objectAtIndex:indexPath.row]valueForKey:@"class_id"]intValue] forKey:@"psel_class"];


        [self databaseOpen];
        assessment_list=[[NSMutableArray alloc]init];
        NSString *qq=[NSString stringWithFormat:@"select * from Assessment where class_id=%d",[[[classNames objectAtIndex:indexPath.row]valueForKey:@"class_id"]intValue]];
        assessment_list=[[database executeQuery:qq]mutableCopy];

        [database close];

        if (classNames.count > 0)
        {
            [btnAddAssesst setHidden:NO];
        }
        else
        {
            [btnAddAssesst setHidden:YES];
        }

        if (!assessment_list.count==0) {
            [tbl_assessment setHidden:NO];
            [btnDelete setHidden:NO];
            [tbl_assessment reloadData];
        }


    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];

    [self.appDelegate.splitViewController viewWillAppear:YES];

        [tbl_assessment reloadData];
    }

    else if (aTableView==tbl_assessment)
    {
           [self.appDelegate.splitViewController viewWillDisappear:YES];
        NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
        [viewControllerArray removeLastObject];

        self.thirdDetailViewController=[[[GameAssessment alloc]init]autorelease];
        [viewControllerArray addObject:self.thirdDetailViewController];
        self.appDelegate.splitViewController.delegate = self.thirdDetailViewController;



        [[NSUserDefaults standardUserDefaults]setInteger:[[[assessment_list objectAtIndex:indexPath.row]valueForKey:@"assessment_id"]intValue] forKey:@"ASSESSMENT_ID"];


        [self databaseOpen];
        assessment_list=[[NSMutableArray alloc]init];
        NSString *qq=[NSString stringWithFormat:@"select * from Assessment where class_id=%d",[[NSUserDefaults standardUserDefaults]integerForKey:@"psel_class"]];
        assessment_list=[[database executeQuery:qq]mutableCopy];


        [database close];

        if (!assessment_list.count==0) {
            [tbl_assessment setHidden:NO];

            [tbl_assessment reloadData];
        }


        [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];


        [self.appDelegate.splitViewController viewWillAppear:YES];

    }

    else {

        [self.appDelegate.splitViewController viewWillDisappear:YES];
        NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
        [viewControllerArray removeLastObject];


        if (indexPath.row==0) {
            self.fifthViewController=[[[ResultStudent_vice alloc]init]autorelease];
            [viewControllerArray addObject:self.fifthViewController];
            self.appDelegate.splitViewController.delegate = self.fifthViewController;
        }

        if (indexPath.row==1) {
            self.fourthDetailViewController=[[[ResultClass_vice alloc]init]autorelease];
            [viewControllerArray addObject:self.fourthDetailViewController];
            self.appDelegate.splitViewController.delegate = self.fourthDetailViewController;
        }


        [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];


        [self.appDelegate.splitViewController viewWillAppear:YES];

    }

}

1 个答案:

答案 0 :(得分:1)

将行保持为选中状态要做两件事

  • - deselectRowAtIndexpath不应该在didselectRowAtIndexpath
  • 保留NSIndexpath的NSMutableArray以存储所选值。当选择一行时,将其添加到数组中,并在-cellForRowAtIndexpath方法中检查索引路径是否在单元格中,如果在那里,则生成[tablecell setSelected:YES];否则没有