How to change background selected color storyboard static cells

前端 未结 3 999
眼角桃花
眼角桃花 2021-01-03 00:48

I have an app with storyboard. In one scene I have a tableview with statics cell content. Is possible to change the background selected color to another color out of the def

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 01:40

    I had the same problem. The solution has two parts:

    1) getting the cells, look at this. 2) changing the background color: you must create a UIView with the background color you need and set it as the selectedBackgroundView of the cell

    For instance, I used this code in the viewDidLoad of the uitableviewcontroller:

    UIView *backgroundSelectedCell = [[UIView alloc] init];
    [backgroundSelectedCell setBackgroundColor:[UIColor colorWithRed:130/256.0 green:169/256.0 blue:171/256.0 alpha:1.0]];
    
    for (int section = 0; section < [self.tableView numberOfSections]; section++)
        for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
        {
            NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];
    
            [cell setSelectedBackgroundView:backgroundSelectedCell];
        }  
    

提交回复
热议问题