为什么我的tableview为每个单元返回相同的Parse图像?

时间:2016-01-28 17:16:45

标签: ios swift uitableview parse-platform

我的tableview返回标题,他们的描述,现在我正在尝试返回图像。它目前只为我的所有单元格返回一个图像。这是因为我将它存储在UIImage中吗?

这是我的代码:

import UIKit
import Parse
import Bolts
import ParseUI

class YourEvents: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var currentuser = PFUser.currentUser()?.username


    //array
    var testArray = [String]()

    var testdecr = [String]()
    var image = UIImage()

   //  var imagestored = UIImage()

    override func viewDidLoad() {
        super.viewDidLoad()
        var query = PFQuery(className:"Companies")
         let pUserName = PFUser.currentUser()?["username"] as? String

        query.whereKey("createdby", equalTo:"\(pUserName)")
        // let runkey = query.orderByAscending("companyname")
        query.findObjectsInBackgroundWithBlock{
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil {
                //do something with the found objects
                if let objects = objects as [PFObject]! {
                    for object in objects {

                        let load = object.objectForKey("companyname") as! String
                        self.testArray .append(load)

                        print(self.testArray)

                        let load2 = object.objectForKey("companydescription") as! String
                        self.testdecr.append(load2)

                        print(self.testdecr)

                         if let userImageFile = object["imagefile"] as? PFFile {
                         userImageFile.getDataInBackgroundWithBlock {
                            (imageData: NSData?, error: NSError?) -> Void in
                            if error == nil {
                                if let imageData = imageData {
                                     self.image = UIImage(data:imageData)!
                                    print("done!")
                                    self.do_table_refresh()

                                }
                            }
                        }

                    }


                        }

                }
             } else {
                //log details of failure
                print("Error: \(error!) \(error?.userInfo) ")

            }


        }

        // reload  UIViewController and UITabkeView
        sleep(3)

        do_table_refresh()
    }

    func do_table_refresh () {
        dispatch_async(dispatch_get_main_queue(),  {
            self.tableView.reloadData()
            return
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("yourstartups", forIndexPath: indexPath) as! YourStartupsCell
        cell.lbTitle!.text = self.testArray[indexPath.row]
        cell.lbDescription!.text = self.testdecr[indexPath.row]
        cell.logo!.image = self.image


        return cell
    }

}

Table View

1 个答案:

答案 0 :(得分:1)

我建议创建一个PFImage对象数组,然后在表视图委托方法中,您只需访问索引路径中当前行的元素即可。现在,您调用数据的方法被调用一次,因此将图像设置为最后一个获取的对象,但由于每次加载单元格时都会调用tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath),因此您需要将图像保存在数组中,就像你正在处理文本标签一样。