2017-04-11 59 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

相关问题