我目前正在开发一款应用,我希望用户能够收藏一个按钮,然后将该按钮添加到“收藏夹”中。特定ViewController
的部分。
该应用程序是一个音板,因此它被分成(当前)2个字符,在这些字符中我有声音文件类别供用户选择,一旦用户选择了一个类别,它将加载{{1}用一组可以按下按钮播放的声音(我知道非常简单)。
我正试图实施一个“收藏夹”'功能是为了让用户喜欢他们选择的按钮,并且这样做,新的' favourited'按钮将以新ViewController
显示。
我浏览了这里,发现了一些关于ViewController
我是Swift和iOS开发的新手,所以如果有人能够引导我朝着正确的方向前进,我将非常感激。
我知道如何设置收藏夹按钮的图像以更改它是否已被设置为收藏夹,我还注意到它建议我将收藏夹放在NSUserDefaults
中。我的应用目前正在使用UITableViewController
,其中包含4个不同的UITabBarController
。
如果你想了解我的故事板的截图以便更好地理解,请告诉我,我会更新帖子,如果需要任何代码请告诉我,我会再次更新帖子!
编辑:我很乐意添加一个按钮,如果有人知道如何将正常按钮添加到表格视图中,则按下后会加载表格视图,因此按下时会显示收藏夹
答案 0 :(得分:1)
尝试按以下步骤操作 -
UITabBarController
viewController
作为soundsViewController
,将另一个tableViewController
作为favouritesTableViewController
,如下图所示 - 现在将这些示例代码添加到soundsViewController
-
var favListArray:NSMutableArray = []
@IBOutlet weak var tableView: UITableView!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if NSUserDefaults.standardUserDefaults().objectForKey("favList") != nil {
favListArray = NSMutableArray.init(array: NSUserDefaults.standardUserDefaults().objectForKey("favList") as! NSMutableArray)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("soundCell", forIndexPath: indexPath) as! TableViewCell
cell.titleLbl.text = NSString.localizedStringWithFormat("Sound %d", indexPath.row) as String
if favListArray.containsObject(cell.titleLbl.text!) {
cell.addTOFVrtBtn.setTitle("-", forState: UIControlState.Normal)
}else{
cell.addTOFVrtBtn.setTitle("+", forState: UIControlState.Normal)
}
cell.addTOFVrtBtn.tag = indexPath.row
cell.addTOFVrtBtn.addTarget(self, action:#selector(addToFav) , forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func addToFav(sender:UIButton) {
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: sender.tag, inSection: 0)) as! TableViewCell
if favListArray.containsObject(cell.titleLbl.text!) {
favListArray.removeObject(cell.titleLbl.text!)
}else{
favListArray.addObject(cell.titleLbl.text!)
}
tableView.reloadData()
NSUserDefaults.standardUserDefaults().setObject(favListArray, forKey: "favList")
}
这是一个示例代码,此处TableViewCell
是UITableViewCell
的子类titleLbl
和addTOFvrtBtn
。
favouritesTableViewController
- varfavSoundList:NSMutableArray = []
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if NSUserDefaults.standardUserDefaults().objectForKey("favList") != nil {
favSoundList = NSMutableArray.init(array: NSUserDefaults.standardUserDefaults().objectForKey("favList") as! NSMutableArray)
print(favSoundList)
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return favSoundList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = favSoundList.objectAtIndex(indexPath.row) as? String
return cell
}
这是o / p屏幕演示 -
我希望这会对您有所帮助,或者您可以了解如何制作收藏夹列表和收藏夹部分。谢谢
答案 1 :(得分:0)
不要认为它有利于按钮,按钮就是您在应用中显示数据的方式 - 您真正喜欢的是数据本身,所以在这种情况下一个特定的声音文件。
因此,您当前的数据是声音文件列表或这些文件的路径。您的收藏夹列表可以完全相同。我想你可能只是从磁盘中获取一个文件列表来显示,所以你的收藏列表需要以不同的方式存储。这可以是用户默认值,也可以是plist文件,无论是它是一个字符串数组,都是存储数组的简单选项。
存储时只需使用键将数组设置为默认值,或将其写入文件。当您回读时,您应该从用户默认值中读取,然后创建mutableCopy
或使用NSMutableArray
来读取文件,以便之后有一个可编辑的实例,您可以在列表中添加和删除收藏夹。
使用按钮表示声音文件的方法可能比您需要的代码和工作量要多得多。表格视图专门用于显示事物列表,并且没有理由表格视图看起来像按钮列表(虽然更灵活,创建的代码更少)。 / p>