向下滚动到第一个类的最后一个方法(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
}
}
RowTableViewController类
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)
这是因为您使用的闭包(或块)的作用域与方法中的其他代码的作用域不同。您需要使用weakSelf而不是简单的“self”的原因是为了避免一个保留周期,可以找到解释这一点的文档here
关于ios - Swift xcode:按下按钮后通过输入文字来更改表格中行/单元格的名称/标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35927646/