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

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

问题描述

我正在尝试从用户标记的 UITableView 中获取行,然后将它们保存为当前用户的解析关系.

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 行收到错误:

I get an error in line 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
        }
    }    
}

编辑 2:

import UIKit

导入解析导入 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")
                }
        }
    }*/

}

编辑 3:

    @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.TypeUITableView.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.

你需要指定一个 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.

您的基本问题是您似乎将表视图用作数据模型 - 表视图应该只是存储在其他数据结构中的数据的视图.例如,对于当前不在屏幕上的单元格,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.

您可以使用

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)

        }

      })
   }
}

您是否已经有一个包含 Parse 中所有 Courses 的数组?

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

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

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