我想根據表格內容創建動態高度UITableViewCell
。如何製作動態高度的UITableViewCell
但我不能。
import UIKit
class WorkItemCell: UITableViewCell{
@IBOutlet weak var item_view: UIView!
@IBOutlet weak var minimum_startday_label: CustomLabel!
@IBOutlet weak var catchcopyLabel: CustomLabel!
@IBOutlet weak var stationLabel: UILabel!
@IBOutlet weak var paymentLabel: UILabel!
@IBOutlet weak var limitLabel: UILabel!
@IBOutlet weak var mainjobLabel: UILabel!
@IBOutlet weak var companyLabel: UILabel!
@IBOutlet weak var workstartdateLabel2: UILabel!
@IBOutlet weak var fav_view: ImageWithView!
@IBOutlet weak var img_seen_view: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
//self.fav_view = ImageWithView.instance()
self.fav_view.checkedImage = UIImage(named: "fav_on")
self.fav_view.uncheckedImage = UIImage(named: "fav_off")
}
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded()
self.catchcopyLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.catchcopyLabel.bounds)
}
private var _workdic: NSDictionary?
var workdic: NSDictionary? {
get {
return _workdic
}
set(workdic) {
_workdic = workdic
if let workdic = workdic {
let workplace = workdic["WorkPlace"] as? String
let companyname = workdic["CompanyName"] as? String
let jobname = workdic["JobName"] as? String
let payment = workdic["Payment"] as? String
let workstartdate = workdic["WorkStartDate"] as? String
let workdatetime = workdic["WorkDateTime"] as? String
let minimumday = workdic["MinimumWorkDay"] as? String
let applyenddate = workdic["ApplyEndDate"] as? String
let catchcopy = workdic["CatchCopy"] as? String
if notnullCheck(catchcopy){
//行間
let attributedText = NSMutableAttributedString(string: catchcopy!)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail
attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
self.catchcopyLabel.attributedText = attributedText
}
self.catchcopyLabel.sizeToFit()
if let payment_constant = payment{
self.paymentLabel.text = payment_constant
}
if notnullCheck(minimumday) && notnullCheck(workstartdate){
self.minimum_startday_label.text = minimumday!+" "+workstartdate!
}else{
self.minimum_startday_label.text = ""
}
if let applyenddate_constant = applyenddate{
self.limitLabel.text = applyenddate_constant
}
if let jobname_constant = jobname{
self.mainjobLabel.text = jobname_constant
}
if let workdatetime_constant = workdatetime{
self.workstartdateLabel2.text = workdatetime_constant
}
if let companyname_constant = companyname{
self.companyLabel.text = companyname_constant
}
self.stationLabel.text = workplace
self.item_view.sizeToFit()
}
}
}
class func heightForRow(tableView: UITableView, workdic: NSDictionary?) -> CGFloat {
struct Sizing {
static var cell: WorkItemCell?
}
if Sizing.cell == nil {
Sizing.cell = tableView.dequeueReusableCellWithIdentifier("WorkItemCell") as? WorkItemCell
}
if let cell = Sizing.cell {
cell.frame.size.width = CGRectGetWidth(tableView.bounds)
cell.workdic = workdic
cell.setNeedsDisplay()
cell.layoutIfNeeded()
let size = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
print(size)
return size.height+1
}
return 0
}
}
上面的代碼有item_view。 它具有所有標籤和圖像。 它設置了4點(頂部,左側,底部,右側),5px的邊距。
我使用上面的單元格作爲數據列表。它有大約5000個計數。
捕獲複製標籤通常設置爲2行句子。 我想更改每個catch_copy_label的高度,item_view高度和單元格高度。
但我不能。 我總是得到相同的高度,26px。
(366.5, 26.0)
(366.5, 26.0)
我該怎麼辦?
我添加了視圖控制器源代碼的一部分。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
if self.int_total == 0{
return self.view.frame.size.height
}else{
if let workdic: AnyObject = workdata.safeObjectAtIndex(indexPath.row){
return WorkItemCell.heightForRow(self.workview, workdic: (workdic as! NSDictionary),base_height:170)
}else{
return 199
}
}
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 199
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.int_total == 0{
return 1
}
return self.workdata.count
}
/*
Cellに値を設定する.
*/
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Cellの.を取得する.
if self.int_total > 0{
let cell = workItemCell(tableView, cellForRowAtIndexPath: indexPath, str_xib: "WorkItemCell")
return cell
}else{
let nocell: NoCountCell = tableView.dequeueReusableCellWithIdentifier("NoCountCell", forIndexPath: indexPath) as! NoCountCell
nocell.conditionButton.addTarget(self, action: "onClickBack:", forControlEvents: .TouchUpInside)
//初期が終わったらfalse
if self.init_loading{
nocell.conditionButton.hidden = true
nocell.messageLabel.hidden = true
}else{
nocell.conditionButton.hidden = true
nocell.messageLabel.hidden = false
}
return nocell
}
}
func workItemCell(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath,str_xib:String) ->WorkItemCell{
let wcell: WorkItemCell = tableView.dequeueReusableCellWithIdentifier(str_xib, forIndexPath: indexPath) as! WorkItemCell
wcell.separatorInset = UIEdgeInsetsZero
wcell.selectionStyle = UITableViewCellSelectionStyle.None
updateCell(wcell, atIndexPath: indexPath)
return wcell
}
func updateCell(cell:UITableViewCell,atIndexPath:NSIndexPath){
}
func showWorkItem(wcell:WorkItemCell,workdic:NSDictionary){
wcell.workdic = workdic
}
'print(size)'的控制檯輸出是什麼? –
我沒有看到你在哪裏設置大小的內容來計算高度? – anhtu
@ShebinKoshy的輸出被以下。(284.5,0.0) (284.5,0.0) (284.5,0.0) (284.5,0.0) (284.5,0.0) (284.5,0.0) (284.5,0.0) –