2017-04-11 39 views
1
sections = [ 
     Section.init(name: "SelectFromOptions", items: dealnameArray), 
     Section(name: "merchant Details", items:merchantdetailsArray), 
     Section(name: "How to use deals", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]), 
     Section(name: "things to remember", items: ["exchange for cash not allowed"]), 
     Section(name: "Cancelation policy", items: ["Once bought cannot exchange"]), 
     Section(name: "what you get", items: ["Capacity buliding courses"]) 
    ] 


func numberOfSections(in tableView: UITableView) -> Int { 
    return sections.count 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return sections[section].items.count 
} 

// 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "cell") 
    cell.textLabel?.numberOfLines = 0 

    cell.textLabel?.text = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row] 
    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return sections[(indexPath as NSIndexPath).section].collapsed! ? 0 : 44.0 
} 

我想在每個部分中添加不同的標籤和按鈕,有可能嗎?通過使用bpove代碼的所有細胞中只包含標籤,我想在我的firt部分如何在tableview的第一部分添加按鈕和標籤,第二部分只包含標籤

回答

0

您需要創建一個自定義單元格寫這個片段做。創建一個名爲CustomCell.swift的新文件。使用此文件中的以下片段

class CustomCell: UITableViewCell { 
    @IBOutlet weak var lbl: UILabel! 
    @IBOutlet weak var btn: UIButton! 
} 

現在轉到故事板並在您的tableview中添加一個新的原型單元格。選擇新創建的單元格,並在身份檢查器中將其設置爲CustomCell的類(第三個選項 - 右上角)。

在屬性檢查器(第四個選項 - 右上角)中將此單元的標識設置爲customCell

添加一個按鈕和一個標籤到這個新創建的單元格,並通過連接檢查器連接插座(最後一個選項 - 右上角)。

回到你的代碼,並在你的cellForRowAtIndexPath,複製下面的代碼片段

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell 
     //Here you can access the button and the label as cell's property using 
     //cell.btn & 
     //cell.lbl 
     return cell 
    } 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
    cell.textLabel?.numberOfLines = 0 

    cell.textLabel?.text = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row] 
    return cell 
} 
0

你可以嘗試這種類型的代碼添加更多標籤和按鈕..

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     var rowCount = 0 
     if section == 0 { 
      rowCount = Data1.count 
     } 
     if section == 1 { 
      rowCount = Data2.count 
     } 
     return rowCount 
    } 



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
if indexpath.section == 0 
{  
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     let ip = indexPath 
     cell.textLabel?.text = Data1[ip.row] as String 
     return cell 
    } 
else if indexpath.section == 1 
{  
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     let ip = indexPath 
     cell.textLabel?.text = Data1[ip.row] as String 
     return cell 
    } 
} 
0

這很簡單。只需使用兩個動態單元格,其中一個僅包含標籤,另一個包含按鈕和標籤(或任何您需要的)。第一節使用一個單元,第二節使用另一個單元。而已。

+0

你能解釋一下嗎? – udaykumar40

+0

通過使用我的代碼 – udaykumar40

0
switch indexPath.section { 
    case 0: 
     //add buttons and labels here 
     //prase data 
     break 
    case 1: 
     //add only labels here 
     //parse data 
     break 
    default: 
     break 
    } 

,或者你可以用如果其他以及

if indexpath.section = 0 { 
    // create button and label here 
    } else { 
    // create label here 
    } 

您需要的cellForRowAtIndexPath

相關問題