AtWork
AtWork

Reputation: 1291

Unable to keep Table row is selected

In my app, I used a splitView. There are 3 different tables.

When I select any row from 1st Table, the display in the detail view & row remains selected in Left side menu. However, when I select any row from 2nd or 3rd Table, the row is just highlighted with blue color & quickly disappear the Selected blue (i.e. it does not remain Highlighted).

Help me solve this problem.

My code as follow:

- (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];

    }

}

Upvotes: 0

Views: 854

Answers (1)

Lithu T.V
Lithu T.V

Reputation: 20021

Two things to do to keep the row as selected

  • -deselectRowAtIndexpath should not be there in didselectRowAtIndexpath
  • Keep an NSMutableArray of NSIndexpath for storing the selected value .When a row is selected add it to array and in the -cellForRowAtIndexpath method check the indexpath is there in cell and if it is there make [tablecell setSelected:YES]; otherwise NO

Upvotes: 1

Related Questions