Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

freshking

macrumors newbie
Original poster
Feb 15, 2009
8
0
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:mad:"NumbersDetailC ell"];
if (cell == nil) {
cell = [[[NumbersDetailCell alloc] initWithFrame:CGRectZero reuseIdentifier:mad:"NumbersDetailCell"] autorelease];
cell.hidesAccessoryWhenEditing = NO;
}

if (indexPath.section == 0) {

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

}

if (indexPath.section == 1) {

cell.flag.image = [UIImage imageNamed:mad:"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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 6502
Sep 17, 2003
310
0
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."
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.