2017-03-06 23 views
0

我從API這樣的響應特性:如何設置頁眉部分的標題從INDEXPATH.ROW

[{ 
    "name":"apple" 
    "type": "Fruit" 
    "taste": sweet 

    } 
    { 
    "name":"lemon" 
    "type": "Fruit" 
"taste": "not tasty" 
}] 

,現在我想設置一節的標題爲名UITable的視圖。

爲了這個,我已經在我的視圖控制器稱此爲:

var modelclass = [FruitsModelclass]() 

override func viewDidLoad() 
{ 
self.loaddata() 
} 

func loaddata() 
{ 
//here i have called the API and sucessfully get the response in data and assign as 

    self.modelclass = data 
    self.tableView.reloadData() 
    } 

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { 
    let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as FruitCell 
    let model = self.Modeldata[indexpath.row] 
    cell.textlabel.text = model.name 
    return cell 
} 

我這是怎麼設置的到cell.How可我設置爲我headerSection ??有很多我的Api中的json對象不是固定的。在這裏,我只提出了兩個任何人都可以幫助我.. ??

+0

你想在tableview的頭部渲染什麼樣的數據? – tofucodes

+0

我認爲在嘗試做這些之前,你已經擁有了所有的tableView委託方法了嗎?即numberOfRowsInSection,numberOfSections和重寫func tableView(tableView:UITableView,titleForHeaderInSection節:Int) - > String? –

+0

是的,我有他們,但請指導我如何做到這一點 – Sam

回答

0

如果你只是想添加標題...

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return "SOME TEXT" //return your text here, may be 
} 
如果你想在標題視圖控件則是這樣的

public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat{ 
     return 50 //height you want of section 
    } 
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{ 

     let vw = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50)) 
//view for header, you can also use view from nib, but thats another matter 
     vw.backgroundColor = UIColor.white 

     let label = UILabel(frame: CGRect(x: 10, y: 0, width: tableView.frame.size.width-10, height: 50)) 
//add label or buttons or what ever u want to view.... 
     label.text = 「SOME TEXT「 

     vw.addSubview(label) 

     return vw 
    } 

而作爲一個側面說明numberOfSections

是小鬼顯示頁眉

+0

你在哪裏設置名稱爲部分標題,?? – Sam

+0

你需要用你需要的任何部分名稱替換「SOME TEXT」 – Abhishek

+0

也許你可以上傳你正在面對的問題的一部分代碼給github並在這裏分享它會更容易 – Abhishek

相關問題