Daniel Lyons
Daniel Lyons

Reputation: 129

Identifier for individual UITableViewCell?

I'm trying to create a really simple app that shows a list of songs in a UITableView. The user can then pick a song and the song will start playing. I haven't yet implemented the AVAudioPlayer. I don't wanna deal with that just yet. For now I'm just using test NSLog statements to test the tableView:didSelectRowAtIndexPath: method. Here's my code.

- (void) tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            NSLog(@"Play Stay");
            break;
        case 1:
            NSLog(@"Play She Was Young");
            break;
        case 2:
            NSLog(@"Play Zombie Song");

        default:
            break;
    }
} else {
    //Uh oh I did not create this section.}

}

Note these cells were created programmatically. I have one prototype cell in interface builder and these three were built programmatically. These are not static cells. They're dynamic cells.

The thing is I'm sure that there must be a better way of doing this. If I change the order of any of these cells then the NSLog statements won't match the UILabel on the cell right? What is the better way to code this? Perhaps create a string ID for each cell. But then where should I store the string ID? Should I store it as a @property of TableViewCell.h? How do you make an identifier for each cell?

I saw a really good solution here but I can't use it because I don't have a static cell to create an IBOutlet with.

Upvotes: 0

Views: 59

Answers (1)

Shankar BS
Shankar BS

Reputation: 8402

instead of that use the information in the cell itself obtained in the method - (void) tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

for example in custom cell u are using somthing like, songName, and songPath(to store path) it is just an example, u are having the outlet in the custom cell for labels,i took strings that will be used by labels inside the cell

   @interface CustomCell : UITableViewCell
   @property (nonatomic, retain) NSString *SongName;//to hold the song name
   @property (nonatomic,retain) NSString *SongPath;//put this it may help for your song detection


in view controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];//you can get the cell itself and all associated properties for the cell after rearranging it won't change 
    NSString *songName = cell.SongName;
    NSLog(@"%@",songName);
    //suppose u want to get to paly use the information in the cell
    //cell.SongPath ->gives the url(location of file) use it to paly song



 }


Upvotes: 1

Related Questions