gpt4 book ai didi

ios - swift xcode : Changing the name/title of a row/cell inside a Table by a text input after pressing a button

转载 作者:行者123 更新时间:2023-11-28 08:43:20 25 4
gpt4 key购买 nike

向下滚动到第一个类 (MainProjectScene) 的最后一个方法。 “PressedSaveAs”函数就是我要解决的方法,和截图错误的方法是一样的。

当用户按下按钮时,文本输入将出现,当您单击“确定”时,它会将用户输入的文本分配给表格的行/单元格的标题。

错误截图:enter image description here

我想分配给行/单元格标题的文本字段的屏幕截图(这将在我按下按钮时发生,因此功能在 PressedSaveAs 方法中): enter image description here

import UIKit



weak var FirstFileNameTextField: UILabel!



class MainProjectScene: UIViewController

{



var row: Row?



override func viewWillAppear(animated: Bool)

{



if let r = row

{



row!.FileName = r.FileName



row!.QuartzImage = r.QuartzImage



}

}



override func viewDidLoad()

{

super.viewDidLoad()



// FacebookButton.addTarget(self, action: "didTapFacebook", forControlEvents: .TouchUpInside)



}



@IBOutlet weak var NewFileButton: UIButton!

@IBOutlet weak var TwoDQuartzButton: UIButton!

@IBOutlet weak var YouTubeButton: UIButton!

@IBOutlet weak var TwitterButton: UIButton!

@IBOutlet weak var OpenFileButton: UIButton!

@IBOutlet weak var SnapChatButton: UIButton!

@IBOutlet weak var FacebookButton: UIButton!

@IBAction func PressedTwoDQuartzButton(sender: UIButton) {



}



@IBAction func PressedSnapchatButton(sender: UIButton){



UIApplication.sharedApplication().openURL(NSURL(string: "https://www.snapchat.com/")!)



}



@IBAction func PressedYouTubeButton(sender: UIButton) {



UIApplication.sharedApplication().openURL(NSURL(string: "https://www.youtube.com/")!)



}



@IBOutlet weak var InstagramButton: UIButton!

@IBAction func PressedFacebookButton(sender: UIButton) {



UIApplication.sharedApplication().openURL(NSURL(string: "http://www.facebook.com")!)



}

@IBAction func PressedInstagramButton(sender: UIButton) {



UIApplication.sharedApplication().openURL(NSURL(string: "https://www.instagram.com/")!)



}



@IBAction func PressedTwitterButton(sender: UIButton) {



UIApplication.sharedApplication().openURL(NSURL(string: "https://twitter.com/")!)



}



@IBOutlet weak var SaveAsButton: UIButton!

@IBAction func PressedSaveAs(sender: UIButton) //this is the save as function that I would like to know how to change



{



//1. Create the alert controller.



var alert = UIAlertController(title: "Name/rename your file:", message: "Enter a filename to name/rename and save your file", preferredStyle: .Alert)



//2. Add the text field. You can configure it however you need.



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



textField.text = "Your file name"

})



alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:

{

(action) -> Void in

let textField = alert.textFields![0] as UITextField

print("Text field: \(textField.text)")

// rows.cell.textLabel?.text = textField.text

CurrentFileName = textField.text!

rows[indexPath.row].FileName = CurrentFileName

}))



// 4. Present the alert.

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





// rows[indexPath.row].FileName = rows.cell.textLabel?.text



// rows[i] = textField.text



}





}

行类

import UIKit





let rows = [

Row(FileName: "File slot 1",

QuartzImage: "Image slot 1"),

Row(FileName: "File slot 2",

QuartzImage: "Image slot 2"),

Row(FileName: "File slot 3",

QuartzImage: "Image slot 3"),

Row(FileName: "File slot 4",

QuartzImage: "Image slot 4"),

Row(FileName: "File slot 5",

QuartzImage: "Image slot 5"),

Row(FileName: "File slot 6",

QuartzImage: "Image slot 6"),

Row(FileName: "File slot 7",

QuartzImage: "Image slot 7"),

Row(FileName: "File slot 8",

QuartzImage: "Image slot 8"),

Row(FileName: "File slot 9",

QuartzImage: "Image slot 9"),

Row(FileName: "File slot 10",

QuartzImage: "Image slot 10"),

Row(FileName: "File slot 11",

QuartzImage: "Image slot 11"),

Row(FileName: "File slot 12",

QuartzImage: "Image slot 12")]



//update contents of the array

//each of the table cell will use the stored information in the array

//



class Row

{

///// enum Type: String {

// }



var FileName: String

var QuartzImage: String



// var type: Type

// var shortDescription: String

// var longDescription: String



init(FileName: String, QuartzImage: String) {

self.FileName = FileName

self.QuartzImage = QuartzImage



}


}

行 TableView Controller 类

import UIKit



var CurrentIndex = 0

var CurrentFileName = ""



class RowTableViewController: UITableViewController {



override func viewDidLoad() {



super.viewDidLoad()



// Uncomment the following line to preserve selection between presentations



// self.clearsSelectionOnViewWillAppear = false



// Uncomment the following line to display an Edit button in the navigation bar for this view controller.



// self.navigationItem.rightBarButtonItem = self.editButtonItem()



}



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 rows.count



}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



let cell = tableView.dequeueReusableCellWithIdentifier("FileSlot", forIndexPath: indexPath)



let filename = rows[indexPath.row].FileName



print(filename)



cell.textLabel?.text = filename



return cell



}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

{



let title = "List of Files"

let message = "You have selected \(rows[indexPath.row].FileName)"



// CurrentIndex = rows[didSelectRowAtIndexPath.row]

var i = 0

CurrentIndex = indexPath.row

CurrentFileName = rows[indexPath.row].FileName

let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

let okayAction = UIAlertAction(title: "Okay", style: .Default, handler: nil)

alertController.addAction(okayAction)

presentViewController(alertController, animated: true, completion: nil)

self.tableView.deselectRowAtIndexPath(indexPath, animated: true)



}

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



// Get the new view controller using [segue destinationViewController].



// Pass the selected object to the new view controller.



if let detailViewController = segue.destinationViewController as? MainProjectScene {



if let cell = sender as? UITableViewCell {



if let indexPath = self.tableView.indexPathForCell(cell) {



detailViewController.row = rows[indexPath.row]



}



}



}



// let index = RowTableViewController.indexPathForSelectedRow()



//let currentCell = RowTableViewController.cellForRowAtIndexPath(RowTableViewController.indexPath) as UITableViewCell



//println(currentCell.textLabel!.text)



}



/*



// Override to support conditional editing of the table view.



override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {



// Return false if you do not want the specified item to be editable.



return true



}



*/



/*



// Override to support editing the table view.



override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {



if editingStyle == .Delete {



// Delete the row from the data source



tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)



} else if editingStyle == .Insert {



// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view



}



}



*/



/*



// Override to support rearranging the table view.



override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {



}



*/



/*



// Override to support conditional rearranging of the table view.



override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {



// Return false if you do not want the item to be re-orderable.



return true

}


}

最佳答案

我认为 indexPath 的范围是问题所在,您需要使用

weakSelf.indexPath (if a class level variable)

这是因为您使用的闭包(或 block )与方法中的其余代码具有不同的作用域。您需要使用 weakSelf 而不是简单的“self”的原因。是为了避免保留循环,可以找到对此进行解释的文档 here

关于ios - swift xcode : Changing the name/title of a row/cell inside a Table by a text input after pressing a button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927646/

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