2017-05-14 117 views
0

我有一個if語句在我的應用程序,我想它改變靜態的tableview單元格的高度。這是我期望的更改靜態表視圖單元格高度

import UIKit 

class DashboardViewController: UITableViewController { 

    @IBOutlet weak var cell: UITableViewCell! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     // Set cell to "show" on date 
     let date = Date() 
     let calendar = Calendar.current 
     let components = calendar.dateComponents([.year, .month, .day], from: date) 

     let year = components.year 
     let month = components.month 
     let day = components.day 

     if(month == 6 && day == 5 && year == 2017) { 

      // Here I want the height to be set to 313 
     } else { 

     // Here I want the height to be set to 0 
     } 




    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



} 

我需要幫助的行標記。謝謝!

+0

您是否嘗試過增加高度限制在電池上,改變你的病情是不變的? –

+0

@AndrezaCristinadaSilva我似乎無法爲單元格添加高度限制 – iFunnyVlogger

回答

0

你可以改變你的tableview的實現代碼如下rowHeight屬性。使用你的例子:

if cell is MyCustomTableViewCell { 
    tableView.rowHeight = 313 
} else { 
    tableView.rowHeight = 0 
} 

此外,爲了有效地改變你需要重新加載tableview中與tableview.reload()或通過其索引路徑刷新該行的單元格的高度:

let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0) 
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top) 

希望這將帶你朝着你想要的解決方案。

0

Alrighty這裏是根據您在聊天中提供的細節的修改。

所以,首先你將需要正確設置你的TableViewController。 Here是我發現的文章,涵蓋了很多表格視圖的基礎知識。

後您設置表視圖:

您的模型需要有某種形式的日期屬性,以匹配日期條件。比方說,比如你的模型看起來是這樣的:

var model = [(text: "here is some text", date: Date())] 

你需要重寫/在你的tableViewController /委託執行功能選項卡tableView(:_ heightForRowAt indexPath:_)。在此功能中,您將檢查來自模型的日期並將其應用於您的細胞高度。

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    let date = model[indexPath.row].date 

    if yourCondition { 
     return 313 
    } else { 
     return = 0 
    } 
} 
+0

如果我這樣做,是不是會將所有單元格都更改爲該單元格? – iFunnyVlogger

+0

@iFunnyVlogger它會檢查所有的單元格,是的。我向你保證,雖然這是做到這一點的正確方法。我可能需要稍微修改我的答案。讓我問一下:你是想擴大一下自來水或者其他性質的細胞嗎?你想改變單元高度的條件是什麼? –

+0

實際上,我的if語句是說如果它是一個特定的日期,表格視圖單元格將是313,否則它是0.下面是代碼... if(month == 5 && day == 14 && year = = 2017){ } else { } – iFunnyVlogger

0

據我瞭解你的問題,如果發生特殊情況,你想改變你的UITableViewCell的高度。對於這種情況,我建議您使用UITableView代理功能,即heightForRowAt indexPath

下面的代碼片段:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
if(month == 6 && day == 5 && year == 2017) { 
// Here I want the height to be set to 313 
} else { 
// Here I want the height to be set to 0 
} 
} 
相關問題