How do I select a UITableViewCell by default?

前端 未结 7 2089
忘了有多久
忘了有多久 2020-12-17 10:50

I have created a UITableView and would like a specific UITableViewCell to appear selected (blue) when the view is loaded.

相关标签:
7条回答
  • 2020-12-17 11:27

    Sometimes, when you're not in a UIViewController, you have no viewWillAppear, and sometimes, you created your UITableView programmatically.

    The easy solution is to implement this delegate method:

    - (void)tableView:(UITableView *)tableView 
            willDisplayCell:(UITableViewCell *)cell 
            forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (self.selectedIndex == indexPath.row) {
            cell.selected = YES;
        }
    }
    

    It's does not work in the cellForRowAtIndexPath because the cell is not yet displayed. and the setSelected method is called just when this one is displayed.

    0 讨论(0)
提交回复
热议问题