Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus on last cell in a UITableview with scrolling animation?

I have a UITableview in my xcode project.In that Comments are listed.How can i focus on a last cell of my TableView with scrolling animation?

like image 830
Ananya Avatar asked Feb 07 '13 04:02

Ananya


People also ask

How do I get top of Tableview?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

Below method will find the last index of you table view and will focus to that cell with an animation

-(void)goToBottom
{
    NSIndexPath *lastIndexPath = [self lastIndexPath];

    [<YOUR TABLE VIEW> scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

Code to find the last index of your tableview.

-(NSIndexPath *)lastIndexPath
{
    NSInteger lastSectionIndex = MAX(0, [<YOUR TABLE VIEW> numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [<YOUR TABLE VIEW> numberOfRowsInSection:lastSectionIndex] - 1);
    return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
}

Add this code somewhere in your view controller

[self performSelector:@selector(goToBottom) withObject:nil afterDelay:1.0];
like image 150
Kannan Prasad Avatar answered Nov 07 '22 03:11

Kannan Prasad


Keep in mind that a UITableView inherits from UIScrollView

-(void)scrollToBottom:(id)sender
{
    CGSize r = self.tableView.contentSize;
    [self.tableView scrollRectToVisible:CGRectMake(0, r.height-10, r.width, 10) animated:YES];
}

execute it like

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(…);
[button addTarget:self action:@selector(scrollToBottom:) forControlEvents:UIControlEventTouchUpInside];

(You dont need to use performSelector:…)


Another solution would be selecting the last row. The table view will take care of the rest.

-(void)scrollToBottom:(id)sender
{
    NSInteger lasSection = [self.tableView numberOfSections] - 1;
    NSInteger lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;

    //this while loops searches for the last section that has more than 0 rows.
    // maybe you dont need this check
    while (lastRow < 0 && lasSection > 0) {
        --lasSection;
        lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;
    }
    //if there is no section with any row. if your data source is sane,
    // this is not needed.
    if (lasSection < 0 && lastRow < 0)
        return;

    NSIndexPath *lastRowIndexPath =[NSIndexPath indexPathForRow:lastRow inSection:lasSection];
    [self.tableView selectRowAtIndexPath:lastRowIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}

the button is the same.

like image 31
vikingosegundo Avatar answered Nov 07 '22 03:11

vikingosegundo