Muthu Sabarinathan
Muthu Sabarinathan

Reputation: 1208

In iOS, selected cell should move to top portion without any disturbance in UITableView

I asked one doubt yesterday, here at this link

In that I got 4 answers. Those 4 answers helped me to solve my problem. But now in same scenario, I have another issue. When I open 3rd cell it opens in same place.

enter image description here

I want to close that cell, so I press the button then it closes successfully. but the issue is after closed, it goes to top position.

enter image description here

How to solve this issue. I think I did mistake in calling this method scrollToRowAtIndexPath. In my previous question, (Refer the 1st line link) I changed a single line in below described method.

- (void)method_Expand:(UIButton*)sender
{
    int_SelectedIndex = sender.tag;
    [tbl_CalendarList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);
    if ( int_TempSelectedIndex != int_SelectedIndex)
    {
        int_TempSelectedIndex = int_SelectedIndex;
    }
    else
    {
        int_TempSelectedIndex = -1;
    }
    [tbl_CalendarList reloadData];
}

enter image description here

Upvotes: 1

Views: 358

Answers (1)

Rok Jarc
Rok Jarc

Reputation: 18875

According to your previous question your method_Expand: is called when custom in-cell button is tapped. Try changing your code to:

- (void)method_Expand:(UIButton*)sender
{
    int_SelectedIndex = sender.tag;

    NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);

    if ( int_TempSelectedIndex != int_SelectedIndex)
    {
        int_TempSelectedIndex = int_SelectedIndex;

        [tbl_BlogList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    else
    {
        int_TempSelectedIndex = -1;
    }
    [tbl_CalendarList reloadData];
}

This way tapped cell will scroll to top only when it is being expanded and not when it is being collapsed.

EDIT: there is another way

- (void)method_Expand:(UIButton*)sender
{
    int_SelectedIndex = sender.tag;

    NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);

    if ( int_TempSelectedIndex != int_SelectedIndex)
    {
        int_TempSelectedIndex = int_SelectedIndex;
    }
    else
    {
        int_TempSelectedIndex = -1;
    }

    [tbl_CalendarList reloadData];

    if (int_TempSelectedIndex >= 0)
    {
        dispatch_async(dispatch_get_main_thread(), ^{
            [tbl_BlogList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0]
                                atScrollPosition:UITableViewScrollPositionTop
                                        animated:YES];
        });
    }
}

Source: this answer

Upvotes: 1

Related Questions