gpt4 book ai didi

ios - 最喜欢的按钮 : pass cell

转载 作者:行者123 更新时间:2023-11-30 12:43:40 25 4
gpt4 key购买 nike

请帮忙!我花了很多时间来创建一个最喜欢的按钮,并且我堆叠了:\,我有一个 TableView 单元格,我想创建一个按钮,当我按下该按钮时,该单元格将出现在最喜欢的选项卡上。我开始创建该按钮,现在当我按下它时它会改变颜色,并且它存储在 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

}

我的收藏夹 View Controller :

  import UIKit
class FavoritesViewController : UITableViewController {

example 1 after you press

这是一个例子:

最佳答案

如果您所说的正是我认为您所说的,我假设您所说的“最喜欢的选项卡”是应用程序的一部分,其中包含所有用户最喜欢的歌曲的 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 中为委托(delegate)方法添加类似的内容

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()来实际保存收藏夹

关于ios - 最喜欢的按钮 : pass cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41924237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com