美文网首页
TableView Cell的折叠效果 swift版本

TableView Cell的折叠效果 swift版本

作者: andy_tu | 来源:发表于2017-06-21 19:21 被阅读0次

TableView Cell的折叠效果 swift版本

ViewController.swift

classViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{

vartableview :UITableView?

varsessionArray :NSMutableArray?

overridefuncviewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

createUi()

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

funccreateUi()

{

sessionArray=NSMutableArray()

for_in0...4{

letgp =group()

gp.bIsShow=false;

gp.cellArray=NSArray(array: [1,2,3,4,5])

sessionArray?.add(gp)

}

lettbRect =CGRect(x:0, y:70, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height-70)

tableview=UITableView(frame: tbRect, style: .grouped)

tableview?.delegate=self

tableview?.dataSource=self

self.view.addSubview(tableview!)

}

publicfunctableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int

{

letgp =sessionArray?[section]as! (group)

if(gp.bIsShow)

{

return(gp.cellArray?.count)!

}

else

{

return0

}

}

publicfunctableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell

{

letcellid ="zdcell"

varcell =tableview?.dequeueReusableCell(withIdentifier: cellid)

if(cell ==nil)

{

cell =UITableViewCell(style: .default, reuseIdentifier: cellid)

}

cell?.textLabel?.text=String(format:"%d",indexPath.row)

returncell!

}

funcnumberOfSections(in tableView:UITableView) ->Int{

return(sessionArray?.count)!

}

functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {

}

functableView(_tableView:UITableView, viewForHeaderInSection section:Int) ->UIView? {

letheadview =HeadView(frame:CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:50))

headview.session= section

lettlLabel =UILabel(frame:CGRect(x:0, y:5, width:UIScreen.main.bounds.width, height:50-10))

tlLabel.backgroundColor=UIColor.gray

lettitle =self.tableView(tableView, titleForHeaderInSection: section)!asString

tlLabel.text= title

headview.addSubview(tlLabel)

letgp =sessionArray?[section]as!group

letwh :CGFloat=30

letdirectionImageV =UIImageView(frame:CGRect(x:UIScreen.main.bounds.width-wh-10, y: (50-wh)*0.5, width: wh, height: wh))

if(gp.bIsShow)

{

directionImageV.image=UIImage(named:"arrow_down.png")

}

else

{

directionImageV.image=UIImage(named:"arrow_right.png")

}

headview.imageV= directionImageV

headview.addSubview(directionImageV)

lettap =UITapGestureRecognizer(target:self, action:#selector(handleTapGesture(sender:)))

headview.addGestureRecognizer(tap)

returnheadview

}

funchandleTapGesture(sender:UITapGestureRecognizer){

letheadv = sender.viewas!HeadView

letsession = headv.session

letgp =sessionArray?[session!]as!group

gp.bIsShow=!gp.bIsShow

self.tableview?.reloadData()

}

functableView(_tableView:UITableView, heightForHeaderInSection section:Int) ->CGFloat

{

return50

}

functableView(_tableView:UITableView, heightForFooterInSection section:Int) ->CGFloat

{

return0.1

}

functableView(_tableView:UITableView, titleForHeaderInSection section:Int) ->String?

{

letgp =sessionArray?[section]as!group

if(gp.bIsShow)

{

returnString(format:"第%d组展开",section+1)

}

else

{

returnString(format:"第%d组折叠",section+1)

}

}

}

classHeadView:UIView{

varsession :Int?

varimageV :UIImageView?

}

GroupInfo.swift

classgroup

{

public

varbIsShow :Bool=false

varcellArray :NSArray?

}

相关文章

网友评论

      本文标题:TableView Cell的折叠效果 swift版本

      本文链接:https://www.haomeiwen.com/subject/qvdimttx.html