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 {
这是一个例子:
答案 0 :(得分:0)
如果你说的是我认为你在说什么,我会假设最喜欢的标签'正如您所说,它是您应用的一部分,其中包含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()
来实际保存收藏夹