2016-10-07 18 views
2

我们能否实现具有动态原型单元的Expandable UITableView,该动态原型单元在其内部扩展了动态数量的单元格?像:动态可扩展表视图

我有'x'的课程在tableView中。在攻读任何课程时,它将扩大“y”个接受该课程的学科。

我在我的tableView没有任何静态单元格。我怎样才能做到这一点?

+1

请参考以下链接:http://stackoverflow.com/questions/11626028/expandable-tableview-in-iphone – KKRocks

+0

让'numberOfSection'到** ** course.count ,每个部分默认包含一行**(即)课程标题**在'didSelect'上保留一些'BOOL'值,这取决于将该特定部分的'numberOfRows'增加到** Subject.count **并且重新加载表 – Gokul

+0

我建议使用[这个库](https://github.com/ujell/YUTableView-Swift),我试过了,它对我来说工作得很好。此外,它确实支持Swift 3. –

回答

0

下面是可扩展的tableview细胞的简单和完整的代码,我使用

@IBOutlet weak var tableView: UITableView! 
struct Data { 
    var title = "" 
    var categories = [String]() 
    var isSelected = false 

    init(title: String, categories: [String]) { 
     self.title = title 
     self.categories = categories 
    } 
} 


var tableData = [Data]() 

override func viewDidLoad() { 

    tableData.append(Data(title: "Click Me", categories: ["Row 1", "Row 2", "Row 3"])); 
    tableData.append(Data(title: "Click Me", categories: ["Row 1", "Row 2", "Row 3"])); 
    tableData.append(Data(title: "Click Me", categories: ["Row 1", "Row 2", "Row 3"])); 
    tableData.append(Data(title: "Click Me", categories: ["Row 1", "Row 2", "Row 3"])); 


    super.viewDidLoad() 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 3 
} 
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let viewHeader = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 50)) 
    viewHeader.backgroundColor = UIColor.whiteColor() 

    let titleLabel = UILabel(frame: CGRectMake(10, 0, self.view.frame.width - 10, 50)) 
    titleLabel.text = tableData[section].title 
    viewHeader.addSubview(titleLabel) 


    let btn = UIButton(frame: CGRectMake(0, 0, self.view.frame.width, 50)) 
    btn.addTarget(self, action: #selector(ViewController.headerDidSelect(_:)), forControlEvents: .TouchUpInside) 
    btn.tag = section 
    viewHeader.addSubview(btn) 

    return viewHeader 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return tableData[section].isSelected ? tableData[section].categories.count : 0 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    cell.textLabel?.text = tableData[indexPath.section].categories[indexPath.row] 
    return cell 
} 


func headerDidSelect(sender: UIButton) { 
    for i in 0..<tableData.count { 
     if tableData[i].isSelected == true { 
      tableData[i].isSelected = false 
      tableView.reloadSections(NSIndexSet(index: i), withRowAnimation: .Fade) 
      if i == sender.tag { 
       return 
      } 
      break 
     } 
    } 
    tableData[sender.tag].isSelected = true 
    tableView.reloadSections(NSIndexSet(index: sender.tag), withRowAnimation: .Fade) 
} 
1

您可以使用ExpyTableView,这使得从给定标细胞的可扩展部分。与iOS 8.0兼容。

所有你需要做的是进口ExpyTableView然后:

class ViewController: ExpyTableViewDataSource, ExpyTableViewDelegate { 

    @IBOutlet weak var expandableTableView: ExpyTableView! 

    // First, set data source and delegate for your table view. 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    expandableTableView.dataSource = self 
    //expandableTableView.delegate = self 
    } 

    // Then return your expandable cell instance from expandingCell data source method. 
    func expandableCell(forSection section: Int, inTableView tableView: ExpyTableView) -> UITableViewCell { 
    // this cell will be displayed at IndexPath with section: section and row 0 
    } 
} 

你可以看到现在你的旧表视图部分是一个可扩展表格视图部分。您还可以下载示例项目并查看更详细的示例。

在示例项目中,有一个适合您需求的特定示例。你只需要添加以下代码:

func numberOfSections(in tableView: UITableView) -> Int { 
     return x //x is your provided number here.(e.g. number of courses) 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return y //y is your provided number here.(number of subjects related to a course) 
    }