【问题标题】:favorite button: pass cell最喜欢的按钮:传递单元格
【发布时间】:2017-06-14 22:06:57
【问题描述】:

请帮忙! 我花了很多时间来创建一个最喜欢的按钮,然后我堆叠:\, 我有一个表格视图单元格,我想创建一个按钮,当我按下按钮时,单元格将出现在收藏选项卡上。 我开始创建该按钮,现在当我按下它时它会改变颜色,并与NSUserDefault 一起存储 .我需要的最后一件事是将单元格传递到我最喜欢的选项卡中。

这是我的代码:

导入 UIKit 导入 AVFoundation 导入 MapKit

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 {

这是一个例子:

【问题讨论】:

  • 问题不清楚你能分享一些屏幕你的意思是“单元格将出现在最喜欢的标签上”吗?
  • 现在当你点击收藏按钮时你想更新标签栏项目徽章?
  • 对不起,不!您必须分享一些代码才能理解传递单元格的含义?
  • 我将更具体地说,我有一个表格视图单元格,我有一个最喜欢的按钮,我希望当您按下按钮时,该单元格将出现在每个应用程序的最喜欢选项卡上,并带有最喜欢的按钮 @MücahitTekin

标签: ios swift uitableview button


【解决方案1】:

如果你说的是我认为你在说的话,我假设你所说的“最喜欢的标签”是你应用程序的一个部分,其中包含所有用户最喜欢的歌曲的 UITableView,你的目标是将用户决定收藏的歌曲移至favoritesTableView,我将调用它,然后您应该执行以下操作。

Favoritebutton 操作内:

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()才能真正保存收藏夹

【讨论】:

  • 嘿,首先谢谢,你能把你的答案与我的代码相匹配吗?@garret kaye
  • 我很乐意这样做,但您需要提供更多代码,但答案是否符合您的要求?
  • 好吧,我想帮助你,但我不会为你编写代码。如果您没有当用户喜欢一首歌曲时的方法处理程序,我建议您制作一个像我在答案中提供的那样。然后,您必须将喜欢的按钮添加到每个 tableViewCell。对不起,我在这里没有太多事情要做,您将不得不自己弄清楚一些事情。看来您有很多问题,我建议您阅读 UITableViews。
  • 我只需要它来传递我刚开始编码的单元格并在这个按钮中堆叠:\
  • 查看我更新的答案,您不能只将一个单元格传递给另一个 UITableView 并简单地将其添加到 tableView。您需要重新创建它。通过更新您的收藏夹视图控制器用于在其 UITableView 中生成单元格的数组并在您的 tableViewController 上调用reloadData(),它将添加您想要的单元格。
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 2012-09-20
  • 1970-01-01
  • 2016-04-04
  • 2019-07-03
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
相关资源
最近更新 更多