个别UITableViewCell的标识符?

时间:2014-02-18 08:12:47

标签: ios7 uitableview

我正在尝试创建一个非常简单的应用程序,在UITableView中显示歌曲列表。然后用户可以选择一首歌并且歌曲将开始播放。我还没有实现AVAudioPlayer。我还不想解决这个问题。现在我只是使用测试NSLog语句来测试tableView:didSelectRowAtIndexPath:方法。这是我的代码。

- (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.}

}

请注意,这些单元格是以编程方式创建的。我在界面构建器中有一个原型单元,这三个是以编程方式构建的。这些不是静态单元格。它们是动态细胞。

事情是我确信必须有更好的方法来做到这一点。如果我更改任何这些单元格的顺序,那么NSLog语句将与单元格上的UILabel不匹配?编码这个的更好方法是什么?也许为每个单元格创建一个字符串ID。但那我应该在哪里存储字符串ID?我应该将它存储为TableViewCell.h的@property吗? 你如何为每个细胞制作一个标识符?

我看到了一个非常好的解决方案here,但我无法使用它,因为我没有静态单元格来创建IBOutlet。

1 个答案:

答案 0 :(得分:1)

而不是使用方法- (void) tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

中获得的单元格本身的信息

例如在自定义单元格中你正在使用像songName和songPath这样的东西(用于存储路径) 这只是一个例子,你在标签的自定义单元格中有插座,我拿了将被单元格内的标签使用的字符串

   @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


在视图控制器中

- (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



 }


相关问题