gpt4 book ai didi

ios - UICollectionView 功能问题

转载 作者:行者123 更新时间:2023-11-30 13:52:28 24 4
gpt4 key购买 nike

我在 VC1 上有一个 UICollectionViewCell,该单元格包含一个图像、一个标签和 3 个按钮。

当我点击单元格时。这会触发我的 didSelectItemAtIndexPath 将我带到编辑项目屏幕。

如何访问每个按钮并将其与我单击的单元格相关联?

因此,如果我添加了 6 个单元格,然后单击单元格 1、按钮 1,它会将我带到包含该人信息的个人简介页面。如果我单击单元格 2 按钮 1,它会将我带到相同的生物 VC,但具有与我单击的单元格相关的不同信息。

我的困惑在于在哪里或如何进行设置?

谢谢!

    import UIKit
import Parse


class TrainersViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, AddNewTrainerViewControllerDelegate {

var trainers: [TrainerArray]


required init?(coder aDecoder: NSCoder) {
trainers = [TrainerArray]()
super.init(coder: aDecoder)
loadTrainerItems()
}

//connection to the collection view
@IBOutlet weak var collectionView: UICollectionView!


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("selected")
saveTrainerItems()

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return trainers.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TrainerCell", forIndexPath: indexPath)
var buttonOne = cell.viewWithTag(10)
buttonOne = indexPath.row



let trainer = trainers[indexPath.row]
configureTrainerForCell(cell, withTrainerArray: trainer)
return cell
}


func configureTrainerForCell(cell: UICollectionViewCell, withTrainerArray trainer: TrainerArray) {

if trainer.trainerImage == nil {

let label = cell.viewWithTag(1000) as! UILabel
trainer.trainerImage = UIImage(named: "defaultImage")
label.text = trainer.name

} else {
let image = cell.viewWithTag(2000) as! UIImageView
image.image = trainer.trainerImage

let label = cell.viewWithTag(1000) as! UILabel
label.text = trainer.name
}
}


override func viewDidLoad() {
super.viewDidLoad()
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//This method adds a new trainer to the trainer array
func addNewTrainerViewController(controller: AddNewTrainerViewController, didFinishAddingItem item: TrainerArray) {

let newRowIndex = trainers.count
trainers.append(item)

let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
let indexPaths = [indexPath]
collectionView.insertItemsAtIndexPaths(indexPaths)

dismissViewControllerAnimated(true, completion: nil)
saveTrainerItems()

}

func addNewTrainerViewController(controller: AddNewTrainerViewController, didFinishDeletingItem item: TrainerArray) {

if let index = trainers.indexOf(item) {

let indexPath = NSIndexPath(forRow: index, inSection: 0)
let indexPaths = [indexPath]
if let _ = collectionView.cellForItemAtIndexPath(indexPath) {

self.trainers.removeAtIndex(index)
self.collectionView.deleteItemsAtIndexPaths(indexPaths)

}
}
dismissViewControllerAnimated(true, completion: nil)
saveTrainerItems()
}

//This Method Edits a Trainer
func addNewTrainerViewController(controller: AddNewTrainerViewController, didFinishEditingItem trainer: TrainerArray) {

if let index = trainers.indexOf(trainer) {

let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = collectionView.cellForItemAtIndexPath(indexPath){

configureTrainerForCell(cell, withTrainerArray: trainer)
}
}
saveTrainerItems()
dismissViewControllerAnimated(true, completion: nil)
}


func addNewTrainerViewControllerDidCancel(controller: AddNewTrainerViewController) {
dismissViewControllerAnimated(true, completion: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "AddTrainer" {

let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! AddNewTrainerViewController
controller.delegate = self

} else if segue.identifier == "EditTrainer" {

let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! AddNewTrainerViewController
controller.delegate = self

if let indexPath = collectionView.indexPathForCell(sender as! UICollectionViewCell) {

controller.trainerToEdit = trainers[indexPath.row]
}
}
}



func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}

func documentsDirectory() -> String {

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
return paths[0]
}


func dataFilePath() -> String {

return (documentsDirectory() as NSString)
.stringByAppendingPathComponent("Trainers.plist")
}

func saveTrainerItems() {

let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(trainers, forKey: "TrainersArray")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}

func loadTrainerItems() {

let path = dataFilePath()
if NSFileManager.defaultManager().fileExistsAtPath(path) {

if let data = NSData(contentsOfFile: path) {

let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
trainers = unarchiver.decodeObjectForKey("TrainersArray") as! [TrainerArray]
unarchiver.finishDecoding()
}
}
}


@IBAction func logOut(sender: AnyObject) {

let alert = UIAlertController(title: "Are You Sure You Want To Log Out?", message: "Please Enter Your Username", preferredStyle: UIAlertControllerStyle.Alert)

alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
}

alert.addAction(UIAlertAction(title: "Log Out", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
let textF = alert.textFields![0] as UITextField

if textF.text! != PFUser.currentUser()?.username {
self.displayGenericAlert("Incorrect Username!", message: "Please Enter a Valid Username")

} else if textF.text! == PFUser.currentUser()?.username {
PFUser.logOut()
_ = PFUser.currentUser()
self.dismissViewControllerAnimated(true, completion: nil)
}
}))

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
if action == true {
self.dismissViewControllerAnimated(false, completion: nil)
}}))

self.presentViewController(alert, animated: true, completion: nil)

}

func displayGenericAlert(title: String, message: String) {

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
}))
self.presentViewController(alert, animated: true, completion: nil)
}

@IBAction func bioSegueButton(sender: AnyObject) {

}

}

最佳答案

您可以从索引 'indexpath.row' 获取数组中的数据。

关于ios - UICollectionView 功能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34094175/

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