Arrays tableview数组索引中的第一个单元格超出范围错误
我有一个tableview,其中第一个单元格与所有其他单元格不同(我在表中有两个自定义单元格使用不同的标识符出列)。代码如下:Arrays tableview数组索引中的第一个单元格超出范围错误,arrays,swift,uitableview,Arrays,Swift,Uitableview,我有一个tableview,其中第一个单元格与所有其他单元格不同(我在表中有两个自定义单元格使用不同的标识符出列)。代码如下: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCel
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
}
}
我正在将数组从注释行的索引错误中删除,并且我还不确定使用[indexath.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中。我试着打印bio和dates作为这些func的结尾,它们返回一个大于0的数字,所以数组应该被填充。知道怎么回事吗?您应该尝试使用第一个单元格作为节头单元格
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("profileCell") as! SbPostsTableViewCell
return cell
}
答案将取决于bio&pictureFile是如何填充的(如果它们确实填充了的话),否?您可以共享您的数组代码吗?您必须使用[indexPath.row-1],因为您想显示第二个单元格中包含数组第一个元素的详细信息。@ScottHunter我已经用数组代码更新了我的问题,请查看是否有任何错误,谢谢!在您使用的函数中,但是在有问题的代码中,您使用的是
bio
;为什么?您是否验证了发生错误时,bio
不是空的?这是个好主意!我试着把它放进去,并将[indexPath.row-1]改回[indexPath.row],但我仍然得到了标题中所述的错误,知道为什么吗?您如何设置numberOfRowsInSection??它应该是这样的:override func tableView(tableView:UITableView,numberOfRowsInSection:Int)->Int{return yourArray.count}它现在似乎在工作,只是标题没有随tableView滚动?有没有办法解决这个问题?感谢您可能正在使用普通表视图:普通表视图可以有一个或多个节,节可以有一行或多行,每个节可以有自己的页眉或页脚标题。当用户滚动浏览包含多行的节时,节的页眉会浮动到表视图的顶部,节的页脚会浮动到底部。“尝试更改为分组表视图。”