Velmurugan
Velmurugan

Reputation: 2303

UITableview select starting cell programmatically

I used UItableview in my iPhone application. I move to next view using Tableviewcell click event. But when I click uinavigation's back button, the tableview cell selection is like as previous click.

But, I need, when I click navigation's back button, then the cursor position move to tableview's starting cell.

That is when I click the back button, the table view automatically select first cell.

Upvotes: 0

Views: 1954

Answers (3)

Ugur Atci
Ugur Atci

Reputation: 167

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedItem = indexPath
    let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "web") as! WebViewController
    webVC.url = self.news?[indexPath.item].url
    webVC.selectItemReturn = selectedItem
    self.present(webVC, animated: true, completion: nil)

}
override func viewWillAppear(_ animated: Bool) {
    self.newsTable.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
    self.newsTable.selectRow(at:selectedItem, animated: false, scrollPosition:.top)
}

When click the back button,UITableview automatically selected cell

Upvotes: 0

Gyani
Gyani

Reputation: 2241

try this ::

- (void)viewWillAppear:(BOOL)animated
{   
    NSIndexPath *ip = [NSIndexPath indexPathForRow: 0 inSection:0];
    [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop];
}

Upvotes: 5

Developer
Developer

Reputation: 6465

I think right now ur Navigation Bar button simply popping the view and u need to push the view so that ur app will reload the view of class containing table.

write this line of code in your viewDidLoad:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title Of Button" style:UIBarButtonItemStyleBordered target:self action:@selector(functionToPushView:)];

Now define the function like this:

-(void)functionToPushView: (id)sender
{
     YourClass *objYourClass = [[YourClass alloc] init];
     [[self navigationController] pushViewController:objYourClass animated:YES];
}

Remember, "YourClass" will be the class which contains the UITableView.

Upvotes: 1

Related Questions