这个是通过OC转换过来的 当时写的时候Swift版本是3.0
原理是标记把每个section的标记全部赋值为0,然后在section中定义同样大小的按钮 ,通过方法来改变section的标记,根据标记来刷新对应的section,即可刷新出数据。
代码如下
import UIKit
class ExpandController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var titleString = ["姓 名","身份证","手机号"]
var emptyArray:NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
emptyArray = NSMutableArray.init();
for _ in 0..<titleString.count {
emptyArray?.add("0");
}
self.view.addSubview(self.table);
// Do any additional setup after loading the view.
}
// 懒加载tableview
lazy var table : UITableView = self.tableView()
func tableView() -> UITableView {
self.table = UITableView(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: UITableViewStyle.grouped);
self.table.delegate = self;
self.table.dataSource = self;
return table;
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3;
}
// 1.2 返回行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (emptyArray!.object(at: section) as AnyObject).isEqual("0"){
return 0;
}else{
return 1;//此处返回的是1 故此所有展示的都是1,项目中根据数据改变此处的数据即可
}
}
//1.3 返回行高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 44;
}
//1.4每组的头部高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44;
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let button = UIButton();
button.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44);
button .titleLabel?.font = UIFont.systemFont(ofSize: 16);
button .setTitleColor(UIColor .red, for: UIControlState.normal);
button.tag = section + 1;
button .backgroundColor = UIColor.orange;
button.addTarget(self, action: #selector(btnClike), for: UIControlEvents.touchUpInside);
let imageView = UIImageView();
imageView.frame = CGRect.init(x: 15, y: 12, width: 20, height: 20)
if (emptyArray!.object(at: section) as AnyObject).isEqual("0"){
imageView.image = UIImage.init(named: "setting");
}else if(emptyArray!.object(at: section) as AnyObject).isEqual("1"){
imageView.image = UIImage.init(named: "财富_select");
}
button.addSubview(imageView);
return button;
}
//1.5每组的底部高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1;
}
//底部视图的定义加载
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil;
}
//1.6 返回数据源
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "identtifier";
var cell = tableView.dequeueReusableCell(withIdentifier: identifier);
if(cell == nil){
cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: identifier);
}
cell?.textLabel?.text = titleString[indexPath.row];
return cell!;
}
/******/
func btnClike(_ btn:UIButton) {
if (emptyArray!.object(at: btn.tag - 1) as AnyObject).isEqual("0"){
emptyArray?[btn.tag - 1] = "1";
}else if(emptyArray!.object(at: btn.tag - 1) as AnyObject).isEqual("1"){
emptyArray?[btn.tag - 1] = "0";
}
self.table.reloadSections(NSIndexSet(index:btn.tag - 1) as IndexSet, with: UITableViewRowAnimation.automatic);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}