Changing color of UITableViewCell on state pressed and state selected

╄→尐↘猪︶ㄣ 提交于 2019-12-10 11:19:13

问题


I am having UITableView with customCell(CustomCell having 2 lables and One ImageView).

In normal Mode I need white color of all the cells.

If user press the particular cell, then color of that cell should be gray(rest of the cells should be white)

And If user release the same cell, Color of that cell should be Orange(rest of the cells should be white)

How can I figure it out?

I have tried this using setSelectedBackground, willSelectRowAtIndexPath and Gestures methods. But can't see these 3 Color states for the same Cell. Any Of the 2 states are working together.

Any Idea how can i achieve the same functionality?

I have implemented same functionality in android using selectors. I want the same functionality in iPhone. Any Help?

Thanks in advance!


回答1:


If you want gray then

cell.selectionStyle=UITableViewCellSelectionStyleGray;  

Or you can set background color on didSelectRow

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor orangeColor]];
}  

If you don't want to reload tableData then you have to save your previousSelected Index value then

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Make oreange cell
    UITableViewCell *presentCell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [presentCell setBackgroundColor:[UIColor orangeColor]];

    //make your previous cellBackgrod to clear, you can make it white as per your requirement
    UITableViewCell *previouscell=(UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedCellIndexPath];
    [previouscell setBackgroundColor:[UIColor clearColor]];

    //save your selected cell index
    previousSelectedCellIndexPath=indexPath;
}



回答2:


Write these two method in your Custom Cell

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.contentView.backgroundColor=[UIColor greenColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.contentView.backgroundColor=[UIColor orangeColor];

}



回答3:


A pretty elegant solution is to override the setHighlighted: method of your tableViewCells.

- (void)setHighlighted:(BOOL)highlighted {
  [super setHighlighted:highlighted];
  if(highlighted) {
    _backView.backgroundColor = [UIColor blueColor];
  }
  else
  {
    _backView.backgroundColor = [UIColor blackColor];
  }
}

UITableView will automatically set the selected cell highlighted @property to YES when the user tap on the cell.

Don't forget to call [_tableView deselectCellAtIndexPath:indexPath animated:NO]; in your didSelect tableView delegate method if you want to make your cell unselected!



来源:https://stackoverflow.com/questions/15199687/changing-color-of-uitableviewcell-on-state-pressed-and-state-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!