import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
var tableView = UITableView()
var datas = NSMutableArray()
//記錄點(diǎn)擊的組的位置
var _sectonIndex:NSInteger?;
//記錄每組是否是否展開的狀態(tài)
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);
// 補(bǔ)全不足線
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)
}
//重寫補(bǔ)全不足線的方法
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;
}
//自定義組頭的內(nèi)容
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;
//添加手勢(shì)
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
}
//自定義組尾的內(nèi)容
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView();
}
//組數(shù)
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.datas.count;
}
//行數(shù)
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
}
//手勢(shì)調(diào)用的方法
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點(diǎn)擊的代理方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//取消選中狀態(tài)
tableView.deselectRowAtIndexPath(indexPath, animated: true);
}
}