【发布时间】: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