0
votes

I’m trying to get rows from a UITableView that have been marked by the user and then save them as Parse relations to the current user.

Here is the code for the IBAction (Save button) and the function:

    @IBAction func saveButton(sender: AnyObject) {
    getCheckmarkedCells(tableView: UITableView,indexPath: NSIndexPath)
}


func getCheckmarkedCells(tableView: UITableView, indexPath: NSIndexPath) {

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.accessoryType == .Checkmark {

            let checkedCourse = cell.textLabel?.text

            var query = PFQuery(className: "Courses")
            query.whereKey("coursename", equalTo: checkedCourse!)
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    // The find succeeded.
                    println("Successfully retrieved \(objects!.count) scores.")
                    // Do something with the found objects

                        }
                    }
                } else {
                    // Log details of the failure
                    println("Error")
                }
            }
        }
    }

I get an error in line 2:

Cannot invoke 'getCheckmarkedCells' with an argument list of type '(tableView: UITableView.Type, indexPath: NSIndexPath.Type)‘

What am I doing wrong?

EDIT:

// Configure cells for Checkmarks
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.accessoryType == .Checkmark
        {
            cell.accessoryType = .None
        }
        else
        {
            cell.accessoryType = .Checkmark
        }
    }    
}

EDIT2:

import UIKit

import Parse import ParseUI

class KurseTableViewController: PFQueryTableViewController {

// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "Courses"
    self.textKey = "coursename"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "Courses")
    query.orderByDescending("coursename")
    return query
}



var selectedRows = NSMutableIndexSet()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {

        if (self.selectedRows.containsIndex(indexPath.row)) {
        cell.accessoryType = .None
        self.selectedRows.removeIndex(indexPath.row)
        } else {
        cell.accessoryType = .Checkmark
        self.selectedRows.addIndex(indexPath.row);
        }
    }    
}




@IBAction func saveButton(sender: AnyObject) {
    //getCheckmarkedCells(tableView: UITableView indexPath: NSIndexPath)
}


/*func getCheckmarkedCells(tableView: UITableView, indexPath: NSIndexPath) {

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.accessoryType == .Checkmark {

            let checkedCourse = cell.textLabel?.text

            var query = PFQuery(className: "Courses")
            query.whereKey("coursename", equalTo: checkedCourse!)
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    // The find succeeded.
                    println("Successfully retrieved \(objects!.count) scores.")
                    // Do something with the found objects

                        }
                    }
                } else {
                    // Log details of the failure
                    println("Error")
                }
        }
    }*/

}

EDIT3:

    @IBAction func saveButton(sender: AnyObject) {

    let currentUser = PFUser.currentUser()
    var selectedObjects = Array<PFObject>()

    let cUserRel = currentUser?.relationForKey("usercourses")

    for object in qObjects {
        cUserRel!.removeObject(object as! PFObject)
        }

    println(selectedRowSets)

    for selectedRows in self.selectedRowSets {
        println("count")
        selectedRows.enumerateIndexesUsingBlock(
            {(index, stop) -> Void in
                // Get object reference
                if self.objects != nil{
                    let anObject = self.objects![index] as! PFObject
                    selectedObjects.append(anObject)
                    println(anObject)
                    cUserRel!.addObject(anObject)
                }
            })
    }

    currentUser?.save()

    navigationController?.popViewControllerAnimated(true)
}
2

2 Answers

1
votes

When you are calling your function you are specifying the parameter types (which is why the error message says that you call it with UITableView.Type and NSIndexPath.Type.

You need to specify instances of a UITableView and an NSIndexPath -

Something like

getCheckmarkedCells(tableview:self.tableView indexPath: someIndexPath);

However, you probably don't want to send a specific index path to this method because you want to scan the entire table, not just look at a specific row.

Your fundamental problem is that you appear to be using your table view as a data model - the tableview should simply be a view of data stored in some other data structure. For example, cellForRowAtIndexPath may return nil for a cell that isn't currently on screen.

You can use an NSMutableIndexSet for storing your selected rows -

var selectedRowSets = [NSMutableIndexSet]() // You will need to add a NSMutableIndexSet to this array for each section in your table

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let selectedRows=self.selectedRowSets[indexPath.section]

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {

        if (selectedRows.containsIndex(indexPath.row)) {
             cell.accessoryType = .None
             selectedRows.removeIndex(indexPath.row)
        } else {
            cell.accessoryType = .Checkmark
            selectedRows.addIndex(indexPath.row);
        }
    }    
}

You should use the same test in cellForRowAtIndexPath to set the accessory on cells as they are reused.

You can retrieve the checked row using

func getSelectedObjects() {

    self.selectedObjects=Array<PFObject>()

    for selectedRows in self.selectedRowSets {

      selectedRows.enumerateIndexesUsingBlock({index, stop in
        // Get object reference
        if self.objects != nil{
            let anObject=self.objects![index] as! PFObject
            self.selectedObjects.append(anObject)

        }

      })
   }
}

Do you already have an array that contains all of the Courses from Parse?

You need to allocate a new NSIndexSet for each section. Remove the selectedSet instance variable and change your loop in objectsDidLoad to

for index in 1...section {
    self.selectedRowSets.append(NSMutableIndexSet())
}
0
votes

Another solution would be to parse through each cell in the tableView at the end and check if it is marked or not. I am assuming you have only one section

let rowCount = tableView.numberOfRowsInSection(0)
let list = [TableViewCell]()

for var index = 0; index < rowCount; ++index {
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as! YourCell
    if cell.accessoryType = .Checkmark{
          list.append(cell)
 }