import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
var tableView = UITableView()
var datas = NSMutableArray()
//记录点击的组的位置
var _sectonIndex:NSInteger?;
//记录每组是否是否展开的状态
var isOpenArray:NSMutableArray?;
override func viewDidLoad() {
super.viewDidLoad()
_sectonIndex = -1;
isOpenArray = NSMutableArray();
for i in 0...10 {
print(i)
let isOpen = false;
isOpenArray?.addObject(NSNumber(bool: isOpen));
}
self.title = "分组"
tableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height));
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableFooterView = UIView()
self.view.addSubview(tableView);
// 补全不足线
if tableView.respondsToSelector(Selector("setSeparatorInset:")){
tableView.separatorInset = UIEdgeInsetsZero
}
if tableView.respondsToSelector(Selector("setLayoutMargins:")){
tableView.layoutMargins = UIEdgeInsetsZero
}
// 解析plist文件
let dic:NSDictionary = NSDictionary(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("friend", ofType: "plist")!))!
print(dic)
let array = dic.objectForKey("list") as!NSMutableArray
datas = array
let friend = array[0].objectForKey("spus")
print(friend)
}
//重写补全不足线的方法
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setLayoutMargins:")){
cell.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setSeparatorInset:")){
cell.separatorInset = UIEdgeInsetsZero
}
}
//组头的高度
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40;
}
//自定义组头的内容
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let seclabel = UILabel(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.size.width,40));
seclabel.text = (" ") + (self.datas[section].objectForKey("name") as? String)!;
seclabel.backgroundColor = UIColor.whiteColor();
seclabel.layer.borderWidth = 0.3
seclabel.layer.borderColor = UIColor.init(colorLiteralRed: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 0.6).CGColor
seclabel.tag = section;
//添加手势
seclabel.userInteractionEnabled = true;
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapAction(_:)));
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
seclabel.addGestureRecognizer(tap);
return seclabel;
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
return 90
}
//自定义组尾的内容
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView();
}
//组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.datas.count;
}
//行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let isOpen = isOpenArray![section] as! Bool;
if isOpen == true{
return self.datas[section].objectForKey("spus")!.count;
}
return 0;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! MyCell!
if (cell == nil){
cell = MyCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell");
}
cell.nameLable.text = (self.datas[indexPath.section].objectForKey("spus") as? NSMutableArray)![indexPath.row].objectForKey("name") as? String
cell.ageLable.text = (self.datas[indexPath.section].objectForKey("spus") as? NSMutableArray)![indexPath.row].objectForKey("age") as? String
cell.iconImageView.image = UIImage(named:((self.datas[indexPath.section].objectForKey("spus") as? NSMutableArray)![indexPath.row].objectForKey("icon") as? String)!)
return cell
}
//手势调用的方法
func tapAction(tap:UITapGestureRecognizer){
let index = tap.view?.tag;
var isOpen = isOpenArray![index!] as! Bool;
isOpen = !isOpen;
isOpenArray![index!] = NSNumber(bool: isOpen);
// UIView.animateWithDuration(0.5) {
self.tableView.reloadData();
//}
}
//cell点击的代理方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//取消选中状态
tableView.deselectRowAtIndexPath(indexPath, animated: true);
}
}