2016-04-26 49 views
0

我有一个UITableView使用自定义单元格填充了MPMediaItems和按钮(等等)。我正在尝试使用特定单元格的MPMediaItem执行操作,而不是通过didSelectRowAtIndexPath轻触哪个按钮。我尝试了各种方法,我相信标签可能是要走的路,但我无法得到正确的实施。我似乎只能得到集合中的第一项或最后一项。处理MPMediaItems时设置标签的正确方法是什么?一些代码如下...自定义单元格中的标记和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() 
    } 

回答

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() 
    } 
+0

现在你的问题解决了吗? –

+0

工作...谢谢! – rocketman240