【问题标题】:first cell in tableview array index out of range errortableview 数组索引中的第一个单元格超出范围错误
【发布时间】:2016-03-11 12:02:47
【问题描述】:

我有一个表格视图,其中第一个单元格与所有其他单元格不同(表中有两个自定义单元格使用不同的标识符出列)。代码如下:

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

    if indexPath.row == 0 {

        let cell = tableView.dequeueReusableCellWithIdentifier("profileCell") as! SbProfileViewCell

        //cell.textArea.text = bio[indexPath.row]

        //pictureFile[indexPath.row].getDataInBackgroundWithBlock { (fileData, error) -> Void in

            if let pic = UIImage(data: fileData!) {

                cell.imageView.image = pic
            }
        }

        return cell

    } else {

        let cell = tableView.dequeueReusableCellWithIdentifier("postCell") as! SbPostsTableViewCell

        cell.date.text = dateFormatter.stringFromDate(dates[indexPath.row - 1])

        pictureFileOne[indexPath.row - 1].getDataInBackgroundWithBlock { (fileOneData, error) -> Void in

            if let downloadedImageOne = UIImage(data: fileOneData!) {

                cell.imageArea.image = downloadedImageOne
            }
        }

        cell.textArea.text = text[indexPath.row - 1]

        return cell
    }
}

我让数组脱离了注释行的索引错误,我也不确定使用 [indexPath.row - 1] 是否会产生我想要的效果(我希望显示 tableview 的第二个单元格数组中的第一个对象),希望我解释一切正常,请帮忙!谢谢

编辑

    var dates = [NSDate]()
    var autoBio = [String]()

    func getPosts() {

    let getPostsQuery = PFQuery(className: "allPosts")

    getPostsQuery.whereKey("userId", equalTo: userNumber)

    getPostsQuery.limit = 15
    getPostsQuery.orderByDescending("createdAt")

    getPostsQuery.findObjectsInBackgroundWithBlock { (posts, error) -> Void in

        if let posts = posts {

            self.dates.removeAll(keepCapacity: true)

            for post in posts {

                self.dates.append(post.createdAt as NSDate!)
            }
        }
        print(self.dates.count)
    }
}

    func getBio() {

    let bioQuery = PFQuery(className: "bios")

    bioQuery.whereKey("userId", equalTo: userNumber)
    bioQuery.limit = 1

    bioQuery.findObjectsInBackgroundWithBlock { (bios, error) -> Void in

        if let bios = bios {

            self.bio.removeAll(keepCapacity: false)

            for bio in bios {

                self.bio.append(bio["about"] as! String)
            }
        }
    }

    print(self.bio.count)
}

我将 getPosts() 和 getBio() 放在 viewDidLoad 中。我尝试打印生物和日期计数作为这些函数的结尾,它们确实返回一个 > 0 的数字,因此应该填充数组。知道有什么问题吗?

【问题讨论】:

  • 答案取决于 bio 和 pictureFile 的填充方式(如果有的话),不是吗?
  • 你能分享你的数组代码吗?并且您必须使用 [indexPath.row-1] 来显示具有数组第一个元素的详细信息的第二个单元格。
  • @ScottHunter 我已经用我的数组代码更新了我的问题,请看看有什么问题,谢谢!
  • 在函数中使用self.bio,但在有问题的代码中使用bio;为什么?您是否确认bio 在发生错误时不为空?

标签: arrays swift uitableview


【解决方案1】:

您应该尝试将第一个单元格用作部分标题单元格...

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCellWithIdentifier("profileCell") as! SbPostsTableViewCell

    return cell

}

【讨论】:

  • 这是个好主意!我尝试将其放入并将 [indexPath.row - 1] 更改回 [indexPath.row] 但我仍然收到标题中所述的错误,知道为什么吗?
  • 你是如何设置你的 numberOfRowsInSection 的?它应该看起来像这样: override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return yourArray.count }
  • 它现在似乎正在工作,除了标题不随表格视图滚动?有什么办法可以解决吗?谢谢
  • 您可能正在使用普通表视图:“普通表视图可以有一个或多个部分,部分可以有一行或多行,并且每个部分可以有自己的页眉或页脚标题。当用户滚动浏览有很多行的部分,部分的标题浮动到表格视图的顶部,部分的页脚浮动到底部。” developer.apple.com/library/ios/documentation/UserExperience/… 尝试更改为分组表视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多