开发者

UITableview select starting cell programmatically

开发者 https://www.devze.com 2023-01-29 16:17 出处:网络
I used UItableview in my iPhone application. I move to next view using Tableviewcell click eve开发者_如何学Pythonnt. But when I click uinavigation\'s back button, the tableview cell selection is like

I used UItableview in my iPhone application. I move to next view using Tableviewcell click eve开发者_如何学Pythonnt. 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.


try this ::

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


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.


 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号