动态可扩展表视图

时间:2016-10-07 10:35:44

标签: ios swift uitableview

我们可以实现具有动态原型单元格的Expandable UITableView,该单元格可以扩展其中的动态单元格数量吗?像:

我在tableView中有'x'个课程。在点击任何课程时,它将扩大接受该课程的'y'科目。

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

2 个答案:

答案 0 :(得分: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)
    }

答案 1 :(得分: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)
}
相关问题