Question

I've got a general question which bothering me every time I'm designing a UITableView.

I want to select/deselect the cell according to a certain parameter. However, setselected method is being called before I initialise the object and then it can't really indicates it's status. Is there any way to call this method just upon click? Or maybe I just need to do everything manually. Namely, design all the cell at first and then assign the UITableViewCell to response to my selector.

moreover, is there any way to make the entire cells selected as default?

Thank you very much!

Was it helpful?

Solution

You can either do it by code

select: [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

deselect: [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

or maybe what you are looking for is having an external parameter that will indicates the real status of the cell

OTHER TIPS

I am not sure if that is what you want, but you can always select and deselect a UITableViewCell with the following methods:

select: [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

deselect: [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

you can make a cell selected by either selecting it in the cellForRowAtIndexPath or by selecting them in the viewWillAppear method of the UITableViewController

Ok.. thank you, got an idea how to design it myself... Kind of customised setSelected.

-(id) initWithCoder:(NSCoder *) aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
     self.highlighted = FALSE;
     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellClicked)];
     tapGesture.numberOfTapsRequired = 1;
     tapGesture.delaysTouchesBegan = YES;
     [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)tableCellClicked
{
        BetCellModeIndex cellMode = [self returnCellModeEnum ];
        switch (cellMode) {
            case Selected:
                cell.mode = notSelected;
                [self setSelectedStyle:  NotSelected];
                break;
            case NotSelected:
                cell.mode = selected;
                [self setSelectedStyle:  Selected];
                break;
            default:
                //TODO: HANDLE THE OTHER CASES
                break;
        }
     }
}

-(void) setSelectedStyle : (BetCellModeIndex) betCellModeIndex
{
switch (cellMode) {
    case Selected:
        self.backgroundColor =  [UIColor greenColor];
        break;
    case NotSelected:
        self.backgroundColor =  [UIColor redColor];
        break;
    default:
        //TODO: HANDLE THE OTHER CASES
        break;

     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top