为tableView部分和tableView单元格swift填充数组

时间:2016-03-14 10:32:56

标签: ios arrays swift uitableview parse-platform

我正在尝试实现像Instagram这样的TableView,每个部分有一行。

我想填充两个数组:

first sectionArray获取部分

功能的行数据

并反对获取该部分的名称。

但是当我尝试填充sectionArray时,我收到一个错误: “致命错误:数组索引超出范围”

你知道如何解决它吗?

谢谢!

    import UIKit
    import ParseUI
    import Parse

    class TableView: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {


        @IBOutlet weak var tableView : UITableView?


        var sectionArray : [[PFFile]] = []


        override func viewDidLoad() {
            super.viewDidLoad()

            self.loadCollectionViewData()

        }


        var object = [PFObject]()

        func loadCollectionViewData() {

            let query = PFQuery(className: "Myclass")

            // Fetch data from the parse platform
            query.findObjectsInBackgroundWithBlock {
                (objects: [PFObject]?, error: NSError?) -> Void in

                // The find succeeded now rocess the found objects into the countries array
                if error == nil {

                    // Clear existing country data
                    self.object.removeAll(keepCapacity: true)

                    // Add country objects to our array
                    if let objects = objects as [PFObject]? {
                        self.object = Array(objects.generate())

                        let index = self.object.count as Int
                        print (index)

                        for i in 1...index {
                            //error here!
                            if let finalImage = self.object[i]["image"] as? [PFFile]
                            {
                                self.sectionArray[i] = finalImage

                                print(self.sectionArray[i])
                            }

                        }
                    }

                    // reload our data into the collection view
                    self.tableView?.reloadData()

                } else {
                    // Log details of the failure
                    print("Error: \(error!) ")
                }
            }
        }

        func numberOfSectionsInTableView(tableView: UITableView) -> Int {

            return sectionArray.count
        }

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


            return sectionArray[section].count

        }

        func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            if section < self.object.count {
                if let namelabel = object[section]["Name"] as? String {
                    return namelabel
                }
            }

            return nil
        }

        func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 30
        }


        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ListControllerViewCell!
            if cell == nil
            {
                cell = ListControllerViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
            }

            if let finalImage = sectionArray[indexPath.section][indexPath.row] as? PFFile   //object[indexPath.row]["image"] as? PFFile
            {

                finalImage.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil
                    {
                        if let imageData = imageData
                            {
                                cell.ImagePromo!.image = UIImage(data:imageData)
                            }

                    }

            }


            if let CommentLabel = sectionArray[indexPath.section][indexPath.row]
            //object[indexPath.row]["Comment"] as? String
            {
                cell.CommentLabel!.text = CommentLabel
                cell.CommentLabel!.adjustsFontSizeToFitWidth = true
            }

            return cell;
        }

    }

2 个答案:

答案 0 :(得分:1)

for in 循环中存在问题:

你应该从0开始,而不是1,所以你对循环的调用如下:

for i in 0..<index

这是&#34;危险&#34;与C风格的循环相比,具有for-in循环。您正在循环正确的次数,但是您的数组大小超过了1,因为您从错误的索引开始。

答案 1 :(得分:0)

尝试添加Exception Breakpoint以准确捕获错误位置,

还要将数据源编辑为

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

       if(sectionArray.count != 0) {
        return sectionArray.count
       } else {
        return 0;
       }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       if(sectionArray.count < section) {
          return sectionArray[section].count
       } else {
          return 0;
       }      
}
相关问题