簡體   English   中英

表格視圖中2個不同的可重復使用單元的不同高度

[英]Different heights for 2 different reusable cells in tableview

我有一個包含2個不同的自定義單元格的tableview,它們都具有自己唯一的可重用單元格標識符。 我需要這些具有不同的高度。 我的cellForRowAtIndexPath看起來像這樣:

  //3
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {

   let PostObject = object as! Post

    if PostObject["fichierimage"] as? PFFile == nil {

        let cell2 = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! cell0Entry

        return cell2

    }
    else {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell9", forIndexPath: indexPath) as! cell4

        return cell

    }

}

這樣可以將所有數據正確加載到表視圖中。 但是,兩個單元格的高度均保持在481,而第一個單元格的高度應為99。我在情節提要中將其高度設置為99,但我認為tableView行高將其覆蓋。 我在heightForRowAtIndexPath嘗試了此操作,但沒有成功,它引發了錯誤並使應用程序崩潰:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let object1 = objectAtIndexPath(indexPath)
    let object2 = object1 as! Post


    // this next line throws the error
    if object2["fichierimage"] as? PFFile == nil {
        return 99
    }
    else {
        return 481
    }
}

錯誤消息顯示:

由於未捕獲的異常'NSRangeException'而終止應用程序,原因:'***-[__ NSArrayM objectAtIndex:]:索引2超出范圍[0 .. 1]

修改

// this next line throws the error
if object2["fichierimage"] as? PFFile == nil {
    return 99
}
else {
    return 481
}

對此:

if let _ = object2["fichierimage"] as? PFFile {
    return 99
}
else {
    return 481
}

告訴我是否可行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM