我正在使用UITableView和原型单元。主表视图是DiveDetails2VC,原型单元通过segue基于DiveItemViewController。
我需要保存来解析结果,并且只能保存行0来解析。我不知道如何设置它,以便将tableview中选择的任何行发送到parse。我知道这是由saveEntry中的行引起的:
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
但我不知道如何将forRow语句设置为允许显示数组中的任何行。只有一个部分。
DiveItemViewController控制器
class DiveItemViewController: UITableViewController, ItemDataProtocol
{
private let NumberOfSections: Int = 1
// MARK: - Public Properties
//
// This property is an object that conforms to the ItemDataSelectedProtocol. We use it to call the
// selectedItem method.
//
var itemDataSelectedDelegate: AnyObject?
private var itemData: Array<String> = Array<String>()
override func viewDidLoad()
{
super.viewDidLoad()
// Adds "+" to dive item selection so divers can add another item to the selected list
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(DiveItemViewController.AddItemButton(_:)))
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
// ---------------------------------------------------------------------------------------------
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell)
if self.itemData.isEmpty == false
{
cell.textLabel?.text = itemData[indexPath.row]
}
parseItem = cell.textLabel!.text!
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return self.NumberOfSections
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return itemData.count
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
if self.itemDataSelectedDelegate?.respondsToSelector(#selector(DiveDetails2VC.itemDataSelectedItem(_:))) != nil
{
(self.itemDataSelectedDelegate as! ItemDataSelectedProtocol).itemDataSelectedItem(indexPath.row)
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
return indexPath
}
func appendData(data: Array<String>) {
}
@IBAction func saveEntry (sender: AnyObject) {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath)
let updateDivelog2Query = PFQuery(className: "divelog")
updateDivelog2Query.whereKey("uuid", equalTo: diveUUID)
updateDivelog2Query.getFirstObjectInBackgroundWithBlock {(objects: PFObject?, error: NSError?) -> Void in
if let updateDivelog2Object = objects {
updateDivelog2Object.setValue (self.itemData[indexPath.row], forKey: cell!.textLabel!.text!)
updateDivelog2Object.pinInBackground()
updateDivelog2Object.saveInBackgroundWithBlock {(done:Bool, error:NSError?) in
if done {
print ("ParseData UPDATED data saved")
} else {
updateDivelog2Object.saveEventually()
}
}}}
}
func itemTitle(title: String)
{
self.navigationItem.title = title
}
@IBAction func AddItemButton (sender: AnyObject) {
let alert = UIAlertController(title: "Add item",
message: "Add a new Item to your list",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.itemData.append(textField!.text!)
self.tableView.reloadData()
self.appendData(self.itemData)
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
tableView.reloadData()
}
func itemData(data: Array<String>)
{
self.itemData = data
}
}
最佳答案
在您的var
中声明aviewController
:
var selectedIndexPath: NSIndexPath?
然后在
didSelectRowAtIndexPath
中,使用所选的var
设置此indexPath
,如下所示:override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if let _ = self.selectedIndexPath {
selectedIndexPath = indexPath
}else{
selectedIndexPath = NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)
}
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
if self.itemDataSelectedDelegate?.respondsToSelector(#selector(DiveDetails2VC.itemDataSelectedItem(_:))) != nil
{
(self.itemDataSelectedDelegate as! ItemDataSelectedProtocol).itemDataSelectedItem(indexPath.row)
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
然后在
selectedIndexPath
方法中使用这样的saveEntry
:@IBAction func saveEntry (sender: AnyObject) {
if let indexPath = selectedIndexPath {
let cell = tableView.cellForRowAtIndexPath(indexPath)
...
}
}
EDIT:Added“)”behind“不安全:indexPath.section”
关于arrays - 变量行的IndexPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39149777/