【问题标题】:Populate an array for the tableView section and the tableView cell, swift快速填充 tableView 部分和 tableView 单元格的数组
【发布时间】:2016-06-29 08:23:11
【问题描述】:

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

我想填充两个数组:

第一个sectionArray获取section函数中的行数据

和对象以获取部分的名称。

但是当我尝试填充 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;
        }

    }

【问题讨论】:

    标签: ios arrays swift uitableview parse-platform


    【解决方案1】:

    你的 for in 循环有问题:

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

    for i in 0..<index
    

    与 C 风格的循环相比,这是 for-in 循环的“危险”。您正在循环正确的次数,但由于您从错误的索引开始,因此您的数组大小超过了 1。

    【讨论】:

      【解决方案2】:

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

      还将您的数据源编辑为,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-04
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多