Parse和Swift。在UITableView中获取带有复选标记的单元格。 “无法调用参数。” [英] Parse and Swift. Get cells in UITableView which have a checkmark. "Cannot invoke argument.“

查看:75
本文介绍了Parse和Swift。在UITableView中获取带有复选标记的单元格。 “无法调用参数。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从UITableView获取已由用户标记的行,然后将它们保存为当前用户的Parse关系。

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.

这是IBAction的代码(保存按钮)和函数:

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

我在第2行收到错误:


无法使用类型为'(tableView:UITableView.Type,indexPath:NSIndexPath.Type)'的参数列表调用'getCheckmarkedCells'。

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

我做错了什么?

编辑:

// 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

import Parse import ParseUI

类KurseTableViewController:PFQueryTableViewController {

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


推荐答案

当您调用函数时,您正在指定参数类型(这就是为什么错误消息表示您使用调用它UITableView.Type NSIndexPath.Type

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.

您需要指定a的实例UITableView和NSIndexPath -

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

类似

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.

您的基本问题是您似乎将表视图用作数据模型 - tableview应该只是存储在其他一些数据结构中的数据视图。例如,对于当前不在屏幕上的单元格, cellForRowAtIndexPath 可能会返回 nil

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.

您可以使用 NSMutableIndexSet 来存储您选择的行 -

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

你应该使用相同的测试在 cellForRowAtIndexPath 中,在重复使用单元格时设置附件。

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)

        }

      })
   }
}

你是否已经有一个包含所有<$ c $的数组c>来自Parse的课程?

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

您需要为每个部分分配一个新的NSIndexSet。删除 selectedSet 实例变量,并将 objectsDidLoad 中的循环更改为

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

这篇关于Parse和Swift。在UITableView中获取带有复选标记的单元格。 “无法调用参数。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆