我只是给你一些代码。用作参考。
class MyInfoView: UIView, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myInfoTableView: UITableView!
var headerNamesArray: [String]?//= ["Personal Details", "Contact Details","Emergency Contacts", "Dependents", "Immigration", "Job", "Salary", "Report-to", "Qualifications", "Memberships"]
var cellDetailsArray:NSDictionary?
var currentExpandedIndex:Int = -1
var isButtonTapped:Bool = false
//MARK:- Register Tableview Method -
func registerTableView()
{
println(self.frame)
myInfoTableView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 64)
myInfoTableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "InfoCellID")
loadTableViewDetails()
}
//MARK:- Loading details from Plist -
func loadTableViewDetails()
{
cellDetailsArray = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("TextFieldNames", ofType: "plist")!)
headerNamesArray = cellDetailsArray!.allKeys as? [String]
println(cellDetailsArray)
}
//MARK:- tableview header button Action -
func headerButtonTapped(sender: AnyObject)
{
isButtonTapped = !isButtonTapped
var anIndex:NSInteger = sender.tag
var indexSet:NSMutableIndexSet = NSMutableIndexSet(index: anIndex)
if anIndex == currentExpandedIndex {
// myInfoTableView.deleteRowsAtIndexPaths( (currentExpandedIndex, withRowAnimation: UITableViewRowAnimation.Right);
currentExpandedIndex = -1
} else if (currentExpandedIndex != -1 ) {
indexSet.addIndex(currentExpandedIndex)
currentExpandedIndex = anIndex
isButtonTapped = true
} else {
currentExpandedIndex = anIndex
}
println(indexSet)
myInfoTableView.reloadSections(indexSet ,withRowAnimation:UITableViewRowAnimation.Middle)
}
}
//MARK:- TableView Delegate and DataSource Methods -
extension MyInfoView: UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return headerNamesArray!.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
cellDetailsArray![headerNamesArray![section]];
return cellDetailsArray![headerNamesArray![section]]!.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("InfoCellID", forIndexPath: indexPath) as! CustomTableViewCell
var headerSection = indexPath.section
cell.userTextField.placeholder = cellDetailsArray![headerNamesArray![headerSection]]![indexPath.row] as? String
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if isButtonTapped && currentExpandedIndex == indexPath.section {
return 40.0
}
else {
return 0.0
}
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.5
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = NSBundle.mainBundle().loadNibNamed("TableHeaderView", owner: self, options: nil) [0] as! TableHeaderView
headerView.headerButton.setTitle(headerNamesArray![section], forState: .Normal)
headerView.applyViewStyle()
headerView.headerButton.tag = section
headerView.headerButton.addTarget(self, action: Selector("headerButtonTapped:"), forControlEvents:.TouchUpInside)
headerView.arrowImageView.image = UIImage(named: "sidearrow.png")
if currentExpandedIndex == section {
headerView.arrowImageView.image = UIImage(named: "downarrow.png")
}
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40.0
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
}