繁体   English   中英

迅速地为tableView部分和tableView单元格填充一个数组

[英]Populate an array for the tableView section and the tableView cell, swift

我正在尝试实现像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;
        }

    }

您在for循环中遇到问题:

您应该从0开始,而不是从1开始,因此对循环的调用如下所示:

for i in 0..<index

与C样式的循环相比,这是带有for-in循环的“危险”。 您循环正确的次数,但是因为从错误的索引开始,所以超出了数组大小1。

尝试添加异常断点以准确捕获错误位置,

同时将您的数据源编辑为

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;
       }      
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM