How to programmatically “tap” a UITableView cell?

后端 未结 5 1044
梦如初夏
梦如初夏 2020-12-25 15:23

I was wondering is there a way that I could have my code \"tap\" a cell in my UITableView in order to reproduce the behaviour specified in the - (void)tableView:(UITab

5条回答
  •  有刺的猬
    2020-12-25 15:38

    if you want to have the cell selected, i.e highlight a specific cell:

    //select first row of first section
    NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];
    

    if you want to additionally trigger actions in your didSelectRowAtIndexPath method, you need to manually call the delegate method, it won't happen automatically:

    [self tableView:self.tableView didSelectRowAtIndexPath:selectedCellIndexPath];
    

提交回复
热议问题