Ios 收藏夹按钮:传递单元格

Ios 收藏夹按钮:传递单元格,ios,swift,uitableview,button,Ios,Swift,Uitableview,Button,救命啊! 我花了很多时间来创建一个最喜欢的按钮,我堆叠:\, 我有一个表格视图单元格,我想创建一个按钮,当我按下按钮时,该单元格将显示在“收藏夹”选项卡上。 我开始创建该按钮,现在当我按下它时,它会改变颜色,并使用NSUserDefault . 我需要做的最后一件事是将单元格传递到我最喜欢的选项卡中 这是我的密码: 导入UIKit 进口AVF基金会 导入地图套件 class BarProfileTableViewController: UITableViewController,MKMapVie

救命啊! 我花了很多时间来创建一个最喜欢的按钮,我堆叠:\, 我有一个表格视图单元格,我想创建一个按钮,当我按下按钮时,该单元格将显示在“收藏夹”选项卡上。 我开始创建该按钮,现在当我按下它时,它会改变颜色,并使用
NSUserDefault
. 我需要做的最后一件事是将单元格传递到我最喜欢的选项卡中

这是我的密码:

导入UIKit 进口AVF基金会 导入地图套件

class BarProfileTableViewController: UITableViewController,MKMapViewDelegate{


    let Favoritebutton: UIButton = UIButton(type: UIButtonType.Custom)


    Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), forState: UIControlState.Normal)
    Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), forState: UIControlState.Selected)

    Favoritebutton.addTarget(self, action: #selector(self.button(_:)), forControlEvents: .TouchUpInside)

 var favorites : [String] = [""]

    Favoritebutton.frame = CGRectMake(90, 90, 35, 35)

     //create a new button
        let Favoritebutton: UIButton = UIButton(type: UIButtonType.Custom)
        //set image for button
        Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), forState: UIControlState.Normal)
        Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), forState: UIControlState.Selected)
        //add function for button
        Favoritebutton.addTarget(self, action: #selector(self.button(_:)), forControlEvents: .TouchUpInside)
        //set frame
        Favoritebutton.frame = CGRectMake(90, 90, 35, 35)

        let barButton = UIBarButtonItem(customView: Favoritebutton)
        //assign button to navigationbar
        self.navigationItem.rightBarButtonItem = barButton

        let defaults = NSUserDefaults.standardUserDefaults()

        let buttonIsSelected = defaults.boolForKey("keyForIsButtonSelected") // If no value exists for this key then false is returned

        Favoritebutton.selected = buttonIsSelected

            }
我的收藏夹视图控制器:

  import UIKit
class FavoritesViewController : UITableViewController {


这里有一个例子:

如果你说的是我认为你在说的话,我假设你所说的“收藏标签”是你的应用程序的一个部分,它包含了所有用户喜爱的歌曲的
UITableView
,你的目标是将用户决定喜爱的歌曲移动到
收藏视图中,我会叫它,然后你应该做以下的事情

在<代码>收藏夹按钮内
操作:

func favoriteSong (_ sender:UIButton) {

  /* Right here you are going to want to insert the song data that is in the current tableViewCell which the user wishes to add to favorites */

  // I am assuming that your data of all your songs is stored in some sort of array

  // You will have to get the song title, artist, ect data from your cell which you can use something like this to access

  let p: CGPoint = sender.convert(CGPoint.zero, to: songTableView)
  var indexPath: IndexPath = songTableView.indexPathForRow(at: p)!
  let cell = songTableView.cellForRow(at: indexPath)!

  // These are made up arrays that I assume you have something along the lines of in your app and for the purpose of this answer we are going to assume that the song title is your UITableViewCell's 'textLabel' and your song artist is your UITableViewCell's 'detailTextLabel'


  favoritesSongTitleArray.append(cell.textLabel!)
  favoritesSongArtistArray.append(cell.detailTextLabel!)

  // Then I am going to assume (alot of assumptions here :/ ) your 'favoritesTableView' gets its data from these arrays and therefor all you have to do is reload the favoritesTableView like this  

  favoritesTableView.reloadData()

}
您应该在UITableViewController中为委托方法提供类似的功能

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  // Put whatever format you wish for the cell here
  let cell =  UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

  cell.textLabel.text = favoritesSongTitleArray[indexPath.row]
  cell.detailTextLabel.text = favoritesSongArtistArray[indexPath.row]

  return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return favoritesSongTitleArray.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

  return 50
}
然后,用户喜欢的单元格应该出现在您的
favoritesTableView
中。我希望这有帮助


PS:从我看到的代码来看,您似乎还需要调用
defaults.synchronize()
来实际保存收藏

问题不清楚您能否共享一些屏幕“收藏标签上会出现单元格”是什么意思?现在当您单击收藏按钮时,您想更新标签栏项目徽章吗?抱歉,没有!你必须分享一些代码才能理解你传递单元格的意思?我会更具体地说,我有一个表格视图单元格,我有一个最喜欢的按钮,我希望当你按下按钮时,单元格会出现在每个应用程序的最喜欢选项卡上,最喜欢的按钮@MücahitTekinhey,首先谢谢,您能将您的答案与我的代码匹配吗?@garret kayeI很乐意这样做,但您需要提供更多的代码,但答案是否符合您的要求?嗯,我想帮助您,但我不会为您编写代码。如果您没有一个方法处理程序来处理用户何时喜欢一首歌,我建议您制作一个类似于我在答案中提供的方法处理程序。然后,您必须将该收藏夹按钮添加到每个tableViewCell。对不起,我没有太多的事情要做,你得自己想办法。似乎您有很多问题,我建议您阅读UITableView。我只需要这些问题来传递我刚开始编码的单元格,并在这个按钮中进行堆叠:\n请参阅我的更新答案,您不能只将单元格传递给另一个UITableView,然后简单地将其添加到tableView。你需要重新创建它。通过更新FavoritesViewController用于在其UITableView中生成单元格的数组,并在tableViewController上调用
reloadData()
,它将添加所需的单元格。