我需要让 tableView 中的第一个单元格与其余单元格的大小不同。我的其余单元格都位于 CustomPFTableViewCell 类下,但第一个单元格是不同的单元格,因此它位于 FirstPFTableViewCell 类下,这两个单元格都从 PFTableViewCell 类扩展。现在,我只是根据 indexPath.row 使用 if 来判断单元格是否是第一个单元格。如果不是,它将从 Parse 加载单元格的数据。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
if(indexPath.row >= 1){
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomPFTableViewCell!
print("Loading Parse Database Files...")
// Extract values from the PFObject to display in the table cell
if let name = object?["Name"] as? String {
cell?.nameTextLabel?.text = name
print("Loading " + name)
}
if let author = object?["authorName"] as? String {
cell?.authorTextLabel?.text = author
}
if let likes = object?["Likes"] as? Int {
let stringVal = String(likes)
cell?.numLikes.text = stringVal
}
if let descrip = object?["Description"] as? String {
cell?.descriptionHolder = descrip
}
let initialThumbnail = UIImage(named: "Unloaded")
cell.customFlag.image = initialThumbnail
if let thumbnail = object?["imageCover"] as? PFFile {
cell.customFlag.file = thumbnail
cell.customFlag.loadInBackground()
}
return cell
}
print("Finished loading!")
let cell = tableView.dequeueReusableCellWithIdentifier("firstCell") as! PFTableViewCell
return cell
}
末尾是空的,因为我不确定如何更改第一个单元格的大小。 (在 Interface Builder 中将其设置为 60)。我想解决这个问题最重要的部分是:
let cell = tableView.dequeueReusableCellWithIdentifier("firstCell") as! PFTableViewCell
return cell
}
最佳答案
为了调整单元格的大小,您必须实现 UITableViewDelegate
函数
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return firstCellHeight
} else {
return customCellHeight
}
关于ios - 第一个单元格大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35367188/