niklassaers
niklassaers

Reputation: 8830

Woes with the selected button state in custom UITableViewCells

I have a custom UITableViewCell with two buttons in it, mycell.m/mycell.h/mycell.xib. In the XIB I have given the buttons a sample label, and I change that text in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *).

Now, when I click the buttons, while I'm holding my finger on it, it will show the sample text instead of the actual text. And should I miss the button and touch inside the custom UITableViewCell, both buttons change text to the sample text, and they don't redisplay the text I set in cellForRowAtIndexPath until I scroll the cell off-screen and back again.

Any clues to what I might be doing wrong? I haven't found any way to set a selected text.

Cheers

Nik

Upvotes: 1

Views: 336

Answers (1)

mackworth
mackworth

Reputation: 5953

Well, trying again...

I set up a mini project and recreated the problem. After puzzling over why the text I set wasn't being used, I finally realized that you can set a different title for every control state (setTitle:forState:). Normally, if you just set titleLabel.text, then you're setting it for UIControlStateNormal, which is then used by default for all other states. However, IB apparently sets the sample title you specify for all the states separately, not just for normal state. So in your code, you're indirectly doing a setTitle:forState:UIControlStateNormal, but the titles set by IB for all the other states are remaining unchanged. So when you hold down the button (UIControlStateSelect) or you hit the cell (UIControlStateHighlight), then the sample label is displayed. SO...either remove the sample label in IBuilder (the easy path), OR set the label for all the relevant states.

Old incorrect answer:

cellForRowAtIndexPath is only called when the tableView needs to display a cell (e.g. when scrolling back onto the screen.) It's not called anytime a cell is on the screen. Sounds like you've got another routine (e.g. buttonPressed) that's set up as an IBAction from the buttons.

Upvotes: 1

Related Questions