Tuesday, June 21, 2016

iOS UITableView keep one row(cell) selected


To keep one row selected in a tableview, add the below code after the table is loaded, (if its on page load, a good place would be to keep the below code in viewDidAppear. It worked well for me!)


UITableViewCell *selectedCell = [myTableView cellForRowAtIndexPath:selectedIndexPath];
if (selectedCell) {
    selectedCell.selected = YES;
}

[self tableView:myTableView didSelectRowAtIndexPath:selectedIndexPath];

[myTableView selectRowAtIndexPath:selectedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];