Вот простой и полный код для расширяемых 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)
}
См следующую ссылку: http://stackoverflow.com/questions/11626028/expandable-tableview-in-iphone – KKRocks
Сделать 'numberOfSection' к ** course.count ** , Каждый раздел содержит одну строку по умолчанию ** (т. Е.) Название курса **. В 'didSelect' сохранить какое-то значение« BOOL »в зависимости от того, что увеличивает число« numberOfRows »в этом конкретном разделе на ** Subject.count ** и перезагрузите таблицу – Gokul
Я предлагаю использовать [эту библиотеку] (https://github.com/ujell/YUTableView-Swift), я попробовал, и это сработало для меня. Кроме того, он поддерживает Swift 3. –