0

I have a table displaying items. Each cell has a button that is used to display something else about the item. In order to track what is being displayed (used in the parent view as the table is in a modal), I set the button to be selected.

My issue is that I want to ensure only ONE button is "selected" at a time. So if user taps button A, then later taps button B, how can I "deselect" button A?

Again, the buttons are embedded in each cell, so how can I iterate through each cell and disable the buttons (WITHOUT reloading the table data)?

4

2 回答 2

0

I guess you save the information about wether a button is pressed or not out of the button. You have to do so, because the cells can be reused, and so if you only save this info in the selected property of a button, it will be lost if the cell is being reused.

Taking into account that you have the index of the selected button cell saved somewhere, you can iterate through all the visible cells in the UITableView using visibleCells method. For each UITableViewCell, you disable the button if its index is not the one you want. Use indexPathForCell: to check the index.

And you should be good. Of course, everytime the dataSource asks for a cell, check if the indexPath is the selected and if so, set the button.selected to YES.

于 2012-11-09T20:24:10.277 回答
0

I think you want to be using UISegementedControls or UISwitches instead of UIButtons. Either of those other controls are set up to automatically deselect all other options when the user chooses one. They also provide very nice methods for knowing what the user has selected, and can be configured so each option appears differently depending on if it's the current selected item or not. Also, they can trigger other code completely unrelated to appearance when the selected item is changed, and the code can be written to deal with each option differently.

于 2012-11-09T20:46:19.720 回答