自定义单元格中的标签和MPMediaItems

时间:2016-04-26 07:05:50

标签: swift uitableview tags custom-cell mpmediaitem

我有一个UITableView使用自定义单元格,其中填充了MPMediaItems和按钮(以及其他内容)。我尝试使用特定单元格的MPMediaItem执行操作,而不是通过didSelectRowAtIndexPath来触发哪个按钮。我已经尝试了各种方法,我相信标签可能是要走的路,但我无法获得正确的实施方案。我似乎只能得到该集合的第一项或最后一项。处理MPMediaItems时设置标记的正确方法是什么?有些代码如下......

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     
    {
         let cell : SongCell = self.songsTableView.dequeueReusableCellWithIdentifier("cell") as! SongCell
         cell.button.addTarget(self, action: #selector(handleSelection), forControlEvents: .TouchUpInside)
         cell.tag = indexPath.row  //removing this line results in getting the first item in the collection, using it results in the last (understandably so)
         index = cell.tag
         return cell
    }

    @IBAction func handleSelection(sender: AnyObject) 
    {
        let songToHandle = tableData.items![index!]
        queryAsCollectionItems?.append()
    }

1 个答案:

答案 0 :(得分:2)

尝试低估代码。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
         let cell : SongCell = self.songsTableView.dequeueReusableCellWithIdentifier("cell") as! SongCell
         cell.button.addTarget(self, action: #selector(handleSelection), forControlEvents: .TouchUpInside)
         cell.button = indexPath.row
         return cell
     }

     @IBAction func handleSelection(sender: AnyObject) {
          println(sender.tag)
          let songToHandle = tableData.items![sender.tag!]
          queryAsCollectionItems?.append()
     }