tableview数组索引中的第一个单元格超出范围错误

时间:2016-03-11 12:02:48

标签: arrays swift uitableview

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

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

我在viewDidLoad中放了getPosts()和getBio()。我尝试打印生物和日期计数作为这些功能的结束,他们确实返回一个数字> 0所以应该填充数组。知道什么是错的吗?

1 个答案:

答案 0 :(得分:0)

您应该尝试将第一个单元格用作Section Header单元格...

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

    return cell

}
相关问题