【问题标题】:Updating Parse array with local array data, Swift, UITableView使用本地数组数据、Swift、UITableView 更新 Parse 数组
【发布时间】:2015-09-09 20:16:55
【问题描述】:

当 tableview 加载时,我检索 Parse 对象“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 异常预处理 + 165 1 libobjc.A.dylib 0x00000001040cabb7 objc_exception_throw + 45 2 风暴 0x0000000100dc2ae3 -[PFAddOperation applyToValue:forKey:] + 0 3 风暴 0x0000000100dd73bd -[PFObject(Private) performOperation:forKey:] + 206 4 风暴 0x0000000100ddea53 -[PFObject addObjectsFromArray:forKey:] + 126 5 风暴 0x0000000100d64596 _TFC5Storm29SearchTeamTableViewController13createNewTeamfS0_FT_T_ + 486 6 风暴 0x0000000100d5ebe4 _TFC5Storm29SearchTeamTableViewController18nextButtonDidPressfS0_FPSs9AnyObject_T_ + 68 7 风暴 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 发送事件:] + 682 14 UIKit 0x0000000102e13541-[UIApplication sendEvent:] + 246 15 UIKit 0x0000000102e20cdc _UIApplicationHandleEventFromQueueEvent + 18265 16 UIKit 0x0000000102dfb59c _UIApplicationHandleEventQueue + 2066 17 核心基础 0x00000001022e7431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x00000001022dd2fd __CFRunLoopDoSources0 + 269 19 核心基础 0x00000001022dc934 __CFRunLoopRun + 868 20 核心基础 0x00000001022dc366 CFRunLoopRunSpecific + 470 21 图形服务 0x000000010647ea3e GSEventRunModal + 161 22 UIKit 0x0000000102dfe8c0 UIApplicationMain + 1282 23 风暴 0x0000000100d4c117 主要 + 135 24 libdyld.dylib 0x0000000104800145 开始 + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

我很抱歉。这是很多要阅读的。这也是我关于堆栈溢出的第一篇文章。有人可以帮忙吗???

【问题讨论】:

  • 另外,解析中没有更新任何内容。
  • 要保存解析还是查询

标签: ios arrays swift parse-platform


【解决方案1】:

您的查询方法效率不高,因为您只下载了图像,但再次进入 didSelectRowAtIndexPath 方法,您试图从中获取用户 objectID。 但是,您应该在控制器视图类之外创建一个结构,其中将包含用户 ID 和用户的图像。

您的查询函数现在看起来像:

struct ModelStructure
{
var id:String!
var images:UIImage!
}

Controller类内部

var arrayofData = [ModelStructure]()  // create an array of that structure 

  func loadUserData()
  {
      var query = PFQuery(className: "")
 query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
    if error != nil
    {
        println(error)
    }
    else
    {
       if let objects = objects as? [PFObject]
       {
           for object in objects
           {
               var imageFile = object["image"] as! PFFile
               imageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) ->Void in
                if error != nil
                {
                    println(error)
                }
                else
                {
                        if let data = imageData
                         {
                           var imagesToget = UIImage(data: data)
                            var oneModel = ModelStructure()
                            oneModel.id = object.objectId
                            oneModel.images = imagesToget
                            self.arrayofData.append(oneModel)
                            self.tableView.reloadData()
                        }
                }

               }
            }
        }

    }

    }

 }

在 didSelectRowAtIndexPath 你有这个+你想做的事

  var cell = tableView.cellForRowAtIndexPath(indexPath)
    var data = self.arrayofData[indexPath.row]
    if cell?.selected == true {
        // add accessory
        cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
        print("row that are being selected \(indexPath.row) \n")
        var selectUser = data.id
        self.selectedArray.append(selectUser)

    }

【讨论】:

  • 谢谢!我想通了
  • 问题解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多