سؤال

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!

هل كانت مفيدة؟

المحلول 2

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;
}

نصائح أخرى

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];

}

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!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top