变量行的IndexPath

时间:2016-08-25 15:54:48

标签: arrays swift uitableview parse-platform nsindexpath

我正在使用带有原型单元格的UITableView。主要的TableView是DiveDetails2VC,原型单元通过segue基于DiveItemViewController。

我需要保存以解析结果,并且只能将第0行保存到解析中。我不知道如何设置它,以便在tableview中选择的任何行被发送到解析。我知道这是由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
}
}

1 个答案:

答案 0 :(得分:2)

var中声明viewController

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)
       ...
    } 
}

编辑:在“inSection:indexPath.section”后面添加了“)”