PDA

View Full Version : TableViewCell Button Sound




freshking
Feb 25, 2009, 02:21 AM
Hi all,

I have a custom tableView in which each cell contains a UIButton. When I press the button of any cell I want to play a system sound for that particular cell.
(I set up the custom cell in a UITableViewCell subclass (NumbersDetailCell) )


- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {

NumbersDetailCell *cell = (NumbersDetailCell *)[tableView dequeueReusableCellWithIdentifier:@"NumbersDetailC ell"];
if (cell == nil) {
cell = [[[NumbersDetailCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"NumbersDetailCell"] autorelease];
cell.hidesAccessoryWhenEditing = NO;
}

if (indexPath.section == 0) {

cell.flag.image = [UIImage imageNamed:@"1.png"];
if (cell.sound.selected == TRUE)
AudioServicesPlaySystemSound (self.one);

}

if (indexPath.section == 1) {

cell.flag.image = [UIImage imageNamed:@"2.png"];
if (cell.sound.selected == TRUE)
AudioServicesPlaySystemSound (self.two);

}

//and so on...


The problem is that the sound plays as soon as the tableView loads and not when I press the button in the cell. Nothing happens when I press the button.
Its probably a stupid mistake but I just can't seem to figure it out. So any help is well appreciated!



dejo
Feb 25, 2009, 08:40 AM
Move the audio playing logic out of the tableView:cellForRowAtIndexPath: method (which is part of the UITableViewDataSource protocol, used for populating the content of the table view) and put it in a tableView:didSelectRowAtIndexPath: method (which is part of the UITableViewDelegate protocol, used for handling actions for the table view).

Pring
Feb 25, 2009, 08:42 AM
Remember to unselect the row after you select it though or Apple will rejected you based on this clause from HI Guidelines:

"Table views provide feedback when users select list items. Specifically, when an item can be selected, the row containing the item highlights briefly when a user selects it to show that the selection has been received. Then, an immediate action occurs: Either a new view is revealed or the row displays a checkmark to indicate that the item has been selected. The row never remains highlighted, because table views do not display persistent selected state."