gpt4 book ai didi

ios - 解析和 swift 。获取 UITableView 中带有复选标记的单元格。 “无法调用参数。”

转载 作者:行者123 更新时间:2023-11-29 01:58:30 25 4
gpt4 key购买 nike

我试图从 UITableView 中获取已被用户标记的行,然后将它们保存为与当前用户的解析关系。

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

    @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 行出现错误:

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

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.TypeNSIndexPath.Type 调用它。

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

类似

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

但是,您可能不想向此方法发送特定的索引路径,因为您想要扫描整个表,而不仅仅是查看特定行。

您的根本问题是您似乎正在使用 TableView 作为数据模型 - TableView 应该只是存储在其他数据结构中的数据的 View 。例如,对于当前不在屏幕上的单元格,cellForRowAtIndexPath 可能会返回 nil

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

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 中使用相同的测试来在重复使用单元格时设置附件。

您可以使用

检索选中的行
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 的数组?

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

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

关于ios - 解析和 swift 。获取 UITableView 中带有复选标记的单元格。 “无法调用参数。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com