-1

I had a problem with table view didSelect method and prepareForSegue. I used SWRevealController in my app. While selecting cell it reveals the view. Sometimes it not worked properly. It takes two taps to reveal the view. A few months back I used old reveal view frame which contains perform block action. Its worked perfectly.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i=0; i<6; i++)
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (i == indexPath.row)
        {
            cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1];
        }
        else
        {
            cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1];
        }
    }
}
phani
  • 105
  • 2
  • 15

3 Answers3

1

Add this code inside of didSelectRowAtIndexPath and didDeselectRowAtIndexPath functions

    dispatch_async(dispatch_get_main_queue(), ^{

   //Write code what you need 

    });

That works for me.

Subramani
  • 477
  • 2
  • 14
0

The problem is with this line

[self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

You are calling the delegate method, which dequeues a reusable cell (I assume, since it's standard behavior). You do not want to dequeue a reusable cell, you want to do something with a cell that is currently displayed at indexPath. To do that use method from UITableView

[tableView cellForRowAtIndexPath:indexPath];

The full code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // grab the selected cell and give it selected color
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1];
}

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath 
{
    // grab the deselected cell (if it's still visible) and give it deselected color
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1];
}

You will also need to set appropriate color in UITableViewDelegate method cellForRowAtIndexPath, since the color you once set will stay in the cell when it's reused.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • I edited the answer, try now. You're overcomplicating things. All you need to do is grab one cell in `didSelect` and give it selected color, and grab one cell in `didDeselect`, and give it deselected color. – mag_zbc Aug 01 '17 at 11:31
  • Also, in your `UITableViewDelegate` method `cellForRowAtIndexPath` you will also need to check whether the cell is selected and give it appropriate color. – mag_zbc Aug 01 '17 at 11:35
  • I am using Static Cells. In my scenario I don't need to call cellForRowAtIndexPath. Here all cells have segues also. Because of segues didDeSelelctRowAtIndexPath not calling. – phani Aug 02 '17 at 05:31
-1

This s completely wrong method to get clicked cell

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

And don't use for loop as well. instead of this you should use

UITableViewCell *cell = [tableView cellForIndexPath:indexPath];

to get correct cell.

Rahul Patel
  • 534
  • 4
  • 17