swift 中的 Tableview 单元格折叠和展开值
Tableview cell collapse and expande value in swift
I want to create tableview cell with collapse and exapnnd. but i got some error . i want some help! below is my code hope you dont mind i am new to swift2
我正在使用 swift2。
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
Want to take sectionArr as header
let sectionArr = ["animal", "bird"] //section
let animalArr = ["dog", "cat"]
let birdArr = ["crow", "cock"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionArr.count
}
//header section
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 80)
// headerView.backgroundColor = [UIColor, clearColor];
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(0, 0, tableView.frame.size.width, 40)
lblTitle.text = sectionArr[section]
headerView.addSubview(lblTitle)
return headerView
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return animalArr.count
} else {
return birdArr.count
}
}
//display view cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//cell1.textLabel!.text=sectionArr[indexPath.row]
cell1.textLabel!.text=animalArr[indexPath.row]
cell1.textLabel!.text=birdArr[indexPath.row]
return cell1
}
save selected indexpath row
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let Toy = sectionArr[indexPath.row]
print(Toy)
}
}
I got my answer! Here, i have share my code of collaps and exapnd cell in tableview .Hope its help !
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
let sectionArr = ["Animal", "Bird"]
let animalArr = ["dog", "cat"]
let birdArr = ["crow", "cock"]
var isBool = false
var sec = 0
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionArr.count
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRectMake(0, 0, tableview.frame.size.width, 80)
//create lable dynamically
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(0, 0, tableview.frame.size.width, 40)
lblTitle.text = sectionArr[section]
headerView.addSubview(lblTitle)
//create button dynamically
let btn: UIButton = UIButton(frame: CGRectMake(0, 0, tableview.frame.size.width, 40))
btn.setTitle(sectionArr[section], forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = section // change tag property
headerView.addSubview(btn)
return headerView
}
func buttonAction(sender: UIButton!)
{
if isBool == false {
sec = sender.tag
isBool = true
tableview.reloadData()
} else {
sec = 0
isBool = false
tableview.reloadData()
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if isBool && sec == 0 {
return animalArr.count
} else {
return 0
}
} else{
if isBool && sec == 1 {
return birdArr.count
} else {
return 0
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
if(indexPath.section == 0)
{
cell1.textLabel!.text=animalArr[indexPath.row]
}
else
{
cell1.textLabel!.text=birdArr[indexPath.row]
}
return cell1
}
}
I want to create tableview cell with collapse and exapnnd. but i got some error . i want some help! below is my code hope you dont mind i am new to swift2
我正在使用 swift2。
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
Want to take sectionArr as header
let sectionArr = ["animal", "bird"] //section
let animalArr = ["dog", "cat"]
let birdArr = ["crow", "cock"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionArr.count
}
//header section
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 80)
// headerView.backgroundColor = [UIColor, clearColor];
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(0, 0, tableView.frame.size.width, 40)
lblTitle.text = sectionArr[section]
headerView.addSubview(lblTitle)
return headerView
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return animalArr.count
} else {
return birdArr.count
}
}
//display view cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//cell1.textLabel!.text=sectionArr[indexPath.row]
cell1.textLabel!.text=animalArr[indexPath.row]
cell1.textLabel!.text=birdArr[indexPath.row]
return cell1
}
save selected indexpath row
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let Toy = sectionArr[indexPath.row]
print(Toy)
}
}
I got my answer! Here, i have share my code of collaps and exapnd cell in tableview .Hope its help !
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
let sectionArr = ["Animal", "Bird"]
let animalArr = ["dog", "cat"]
let birdArr = ["crow", "cock"]
var isBool = false
var sec = 0
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionArr.count
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRectMake(0, 0, tableview.frame.size.width, 80)
//create lable dynamically
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(0, 0, tableview.frame.size.width, 40)
lblTitle.text = sectionArr[section]
headerView.addSubview(lblTitle)
//create button dynamically
let btn: UIButton = UIButton(frame: CGRectMake(0, 0, tableview.frame.size.width, 40))
btn.setTitle(sectionArr[section], forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = section // change tag property
headerView.addSubview(btn)
return headerView
}
func buttonAction(sender: UIButton!)
{
if isBool == false {
sec = sender.tag
isBool = true
tableview.reloadData()
} else {
sec = 0
isBool = false
tableview.reloadData()
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if isBool && sec == 0 {
return animalArr.count
} else {
return 0
}
} else{
if isBool && sec == 1 {
return birdArr.count
} else {
return 0
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
if(indexPath.section == 0)
{
cell1.textLabel!.text=animalArr[indexPath.row]
}
else
{
cell1.textLabel!.text=birdArr[indexPath.row]
}
return cell1
}
}