在Swift中,我创建了两个独立的VC,一个是UITextView
,另一个是UITableView
。UITableView
就像一个项目列表,可以单独滑动到编辑/删除。如果选择编辑,它将转到UITextView
。通过使用CoreData(我碰巧是新用户),我想通过在UITableView
行上滑动Edit来再次检索UITextView
中的文本/数据。因此,基本上,如果已经创建了UITableViewRow,并且在其UITextView
中添加了文本,那么TableRow(项目)如何可能专门指向保存的UITextView
?我只是想知道,这可能吗?这就是我未来的计划:
这样做:
- 当用户创建一个新项目(在
UITableView
中(作为一行))时,这将创建一个可以编辑、保存和删除的全新UITextView - 当用户选择编辑现有的
UITableViewRow
(项目)时,它将使用创建时的数据切换到UITextView
StackOverflow用户可以告诉我什么来指导我吗?也许我会用什么来说明细节等等。
非常感谢!感谢
更新,代码:
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate, UITextFieldDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var toolbar: UIToolbar!
@IBOutlet weak var addButton: UIBarButtonItem!
@IBOutlet weak var createButton: UIBarButtonItem!
var projects: [String] = []
var deleted: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "FourthViewController" {
let destination = segue.destinationViewController as FourthViewController
destination.deletedItems = deleted
} else {
fatalError("unhandled segue "(segue.identifier)"")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// Use this for swipe table view cell
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.deleted.append(self.projects[indexPath.row])
projects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return projects.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
// Set the label text as the project name
cell.textLabel!.text = projects[indexPath.row]
return cell
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 1 {
if let name = alertView.textFieldAtIndex(0)?.text {
projects.append(name)
tableView.reloadData()
}
}
}
// Add button
@IBAction func addButtonPressed(sender: AnyObject) {
var alert = UIAlertView(title: "Enter the title", message: "This will be the name of your project", delegate: self, cancelButtonTitle: "Cancel")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("Continue")
alert.show()
}
// Swipe
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
// Delete
var deleteButton = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
self.tableView.dataSource?.tableView?(self.tableView, commitEditingStyle: .Delete, forRowAtIndexPath: indexPath)
println(self.projects)
println(self.deleted)
return
})
deleteButton.backgroundColor = UIColor.redColor()
// Edit
var editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let editMenu = UIAlertController(title: nil, message: "Edit this project", preferredStyle: .ActionSheet)
let editAction = UIAlertAction(title: "Edit", style: UIAlertActionStyle.Default, handler: { action in
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as ThirdViewController
self.presentViewController(vc, animated: true, completion: nil)
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
editMenu.addAction(editAction)
editMenu.addAction(cancelAction)
self.presentViewController(editMenu, animated: true, completion: nil)
})
editAction.backgroundColor = UIColor.lightGrayColor()
return [deleteButton, editAction]
}
}
TextView的代码:
import UIKit
class ThirdViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var myTextView: UITextView!
@IBOutlet weak var editButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// Combine the new text with the old
let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
// Create attributed version of the text
let attributedText = NSMutableAttributedString(string: combinedText)
attributedText.addAttribute(NSFontAttributeName, value: textView.font, range: NSMakeRange(0, attributedText.length))
// Get the padding of the text container
let padding = textView.textContainer.lineFragmentPadding
// Create a bounding rect size by subtracting the padding
// from both sides and allowing for unlimited length
let boundingSize = CGSizeMake(textView.frame.size.width - padding * 2, CGFloat.max)
// Get the bounding rect of the attributed text in the
// given frame
let boundingRect = attributedText.boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
// Compare the boundingRect plus the top and bottom padding
// to the text view height; if the new bounding height would be
// less than or equal to the text view height, append the text
if (boundingRect.size.height + padding * 2 <= textView.frame.size.height){
return true
}
else {
return false
}
}
@IBAction func Edit(sender: AnyObject) {
editButton.title = "Save"
}
}
这绝对是可行的。我相信有多种方法可以实现这一点,但是,我建议通过prepareForSegue传递这些数据。
您要做的第一件事是在textViewController上创建一个var来存储传递的数据对象(这需要是所发送对象类型的var)。一旦创建了这个var,就可以在prepareForSegue调用中获取数据,然后将其传递给textViewControllers var.
例如:
NSManaged对象
import Foundation
import CoreData
@objc(Project)
class Project: NSManagedObject {
@NSManaged var text: String
@NSManaged var person: Person
}
import Foundation
import CoreData
@objc(Person)
class Person: NSManagedObject {
@NSManaged var name: String
@NSManaged var tasks: NSSet
}
TableViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
// First get the cell that was selected
let cell = sender as UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
// Next setup your destination view controller
let textViewVC = segue.destinationViewController as ProjectTextViewVC
// Now fetch the individual object for the select cell
let project = fetchedResultsController.objectAtIndexPath(indexPath!) as Project // Replace 'Project' with the name of your NSManagedObject
// Last thing is to pass over the object to the destination view controller.
textViewVC.project = project
}
TextViewController
class ProjectTextViewController: UITextViewController {
var project: Project? = nil
override func viewDidLoad() {
super.viewDidLoad()
textArea.text = project.text
}
}