使用本地数组数据Swift,UITableView更新Parse数组

时间:2015-09-09 20:16:56

标签: ios arrays swift parse-platform

当tableview加载时,我检索解析对象“User”,并将其加载到userArray中。

 func loadUserData(){


    var query = PFUser.query()


    query?.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error != nil {

            println(error)

        } else if let objects = objects as? [PFObject] {

            println(objects)


            // loop through results. print(results)
            for object in objects {
                self.userArray.addObject(object)


                // code to download an image
                let imageFile = object["image"] as! PFFile

                imageFile.getDataInBackgroundWithBlock {

                    (imageData: NSData?, error: NSError?) -> Void in
                    if error != nil {
                        println(error)
                    } else {

                        if let data = imageData {
                            self.images.append(UIImage(data: data)!)
                            self.tableView.reloadData()



                        }
                    }

                }

            }

            self.tableView.reloadData()
        }

    }

}

当用户选择tableView单元格时,该PFUser.objectId将被发送到[selectedFriends]数组中,该数组是在类顶部创建的。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     let cell = self.tableView.cellForRowAtIndexPath(indexPath)
    let row = indexPath.row
    let cellDataParse : PFObject = self.userArray.objectAtIndex(row) as! PFObject
    var user = userArray[row] as! PFUser
    var iD = user.objectId

    var selectedFriend = iD
    if cell?.selected == true {
    cell!.accessoryType = .Checkmark
        // change made here ----????
    selectedFriends.append(selectedFriend!)
    println(selectedFriends)

    }

    if cell?.selected == false && selectedFriends.count == 2 {
            selectedFriends.removeAtIndex(1)
        }


 if self.selectedFriends.count > 5 {

var alert = UIAlertController(title: "Team Maximum", message: "Max team size is 5 friends\n You can only choose maximum 4 friends for your team.", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: { action in


    self.selectedFriends.removeLast()
    cell?.accessoryType = .None
    cell?.selected = false





    })

}

    }

此时我可以成功选择indexPath.row,并将用户的objectId添加到selectedFriends数组中(我在我的类顶部声明)。

我的目标是将所有objectId添加到数组中,并将它们发送到名为“team”的Parse数组。

以下是此代码:

 func createNewTeam(){

    var user = PFUser.currentUser()
    if selectedFriends.count > 0 {
    user!.addObjectsFromArray([selectedFriends], forKey: "team")
    user!.saveInBackground()
    }
    tableView.reloadData()
    selectedFriends.removeAll(keepCapacity: true)
    println("something should have happend")
}

然后我将此功能添加到动作按钮。当我按下按钮时出现此错误:

2015-09-09 12:52:56.651 Storm [11039:1498653] *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'上一次操作后操作无效。' * 第一次抛出调用堆栈: (     0 CoreFoundation 0x00000001023b3c65 exceptionPreprocess + 165     1 libobjc.A.dylib 0x00000001040cabb7 objc_exception_throw + 45     2 Storm 0x0000000100dc2ae3 - [PFAddOperation applyToValue:forKey:] + 0     3 Storm 0x0000000100dd73bd - [PFObject(Private)performOperation:forKey:] + 206     4 Storm 0x0000000100ddea53 - [PFObject addObjectsFromArray:forKey:] + 126     5 Storm 0x0000000100d64596 _TFC5Storm29SearchTeamTableViewController13createNewTeamfS0_FT_T_ + 486     6风暴0x0000000100d5ebe4 _TFC5Storm29SearchTeamTableViewController18nextButtonDidPressfS0_FPSs9AnyObject_T_ + 68     7 Storm 0x0000000100d5ecd6 _TToFC5Storm29SearchTeamTableViewController18nextButtonDidPressfS0_FPSs9AnyObject_T_ + 54     8 UIKit 0x0000000102dffd62 - [UIApplication sendAction:to:from:forEvent:] + 75     9 UIKit 0x0000000102dffd62 - [UIApplication sendAction:to:from:forEvent:] + 75     10 UIKit 0x0000000102f1150a - [UIControl _sendActionsForEvents:withEvent:] + 467     11 UIKit 0x0000000102f108d9 - [UIControl touchesEnded:withEvent:] + 522     12 UIKit 0x0000000102e4c958 - [UIWindow _sendTouchesForEvent:] + 735     13 UIKit 0x0000000102e4d282 - [UIWindow sendEvent:] + 682     14 UIKit 0x0000000102e13541 - [UIApplication sendEvent:] + 246     15 UIKit 0x0000000102e20cdc _UIApplicationHandleEventFromQueueEvent + 18265     16 UIKit 0x0000000102dfb59c _UIApplicationHandleEventQueue + 2066     17 CoreFoundation 0x00000001022e7431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17     18 CoreFoundation 0x00000001022dd2fd __CFRunLoopDoSources0 + 269     19 CoreFoundation 0x00000001022dc934 __CFRunLoopRun + 868     20 CoreFoundation 0x00000001022dc366 CFRunLoopRunSpecific + 470     21 GraphicsServices 0x000000010647ea3e GSEventRunModal + 161     22 UIKit 0x0000000102dfe8c0 UIApplicationMain + 1282     23风暴0x0000000100d4c117主+ 135     24 libdyld.dylib 0x0000000104800145 start + 1     25 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止

我道歉。这是很多东西。这也是我关于堆栈溢出的第一篇文章。有人可以帮忙???

0 个答案:

没有答案