我对编码很新,我只知道Swift.我找到了几个教程来生成表格中的下拉部分.基本上它将代表一个电视节目,标题将是季节和每个季节的剧集下拉列表.
我设法让这个工作完全符合我想要的https://github.com/fawazbabu/Accordion_Menu
这一切看起来都不错,但我需要能够从下拉项目中进行选择.我添加了didSelectRowAtIndexPath,只是简单的行打印开始.当我选择行,节或单元格时,会返回随机索引路径,可以再次按下同一行并返回不同的值.我认为这只是我在问题中添加的内容.所以我将didSelectRowAtIndexPath添加到原始代码中.这有同样的问题.
我假设这是因为正在使用UIGestureRecogniser以及didSelectRowAtIndexPath.但我不确定替代方案是什么.
有人能告诉我哪里出错了吗?
import UIKit
import Foundation
class test: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var sectionTitleArray : NSMutableArray = NSMutableArray()
var sectionContentDict : NSMutableDictionary = NSMutableDictionary()
var arrayForBool : NSMutableArray = NSMutableArray()
override func awakeFromNib() {
super.awakeFromNib()
tableView.delegate = self
tableView.dataSource = self
arrayForBool = ["0","0"]
sectionTitleArray = ["Pool A","Pool B"]
var tmp1 : NSArray = ["New Zealand","Australia","Bangladesh","Sri Lanka"]
var string1 = sectionTitleArray .objectAtIndex(0) as? String
[sectionContentDict .setValue(tmp1,forKey:string1! )]
var tmp2 : NSArray = ["India","South Africa","UAE","Pakistan"]
string1 = sectionTitleArray .objectAtIndex(1) as? String
[sectionContentDict .setValue(tmp2,forKey:string1! )]
self.tableView.registerNib(UINib(nibName: "CategoryNaMetableCell",bundle: NSBundle.mainBundle()),forCellReuseIdentifier: "CategoryNaMetableCell")
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTitleArray.count
}
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{
if(arrayForBool .objectAtIndex(section).boolValue == true){
var tps = sectionTitleArray.objectAtIndex(section) as! String
var count1 = (sectionContentDict.valueForKey(tps)) as! NSArray
return count1.count
}
return 0;
}
func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
return "ABC"
}
func tableView(tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(arrayForBool .objectAtIndex(indexPath.section).boolValue == true){
return 50
}
return 2;
}
func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let cell = self.tableView.dequeueReusableCellWithIdentifier("CategoryNaMetableCell") as! CategoryNaMetableCell
cell.downArrow.hidden = false
cell.backgroundColor = UIColor.blackColor()
cell.tag = section
cell.CategoryLabel.text = sectionTitleArray.objectAtIndex(section) as? String
let cellTapped = UITapGestureRecognizer (target: self,action:"sectionHeaderTapped:")
cell .addGestureRecognizer(cellTapped)
return cell
}
func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
println("Tapping working")
println(recognizer.view?.tag)
var indexPath : NSIndexPath = NSIndexPath(forRow: 0,inSection:(recognizer.view?.tag as Int!)!)
if (indexPath.row == 0) {
var collapsed = arrayForBool .objectAtIndex(indexPath.section).boolValue
collapsed = !collapsed;
arrayForBool .replaceObjectAtIndex(indexPath.section,withObject: collapsed)
var range = NSMakeRange(indexPath.section,1)
var sectionToReload = NSIndexSet(indexesInRange: range)
self.tableView .reloadSections(sectionToReload,withRowAnimation:UITableViewRowAnimation.Fade)
tableView.reloadData()
}
}
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CategoryNaMetableCell") as! CategoryNaMetableCell
var manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue
if (!manyCells) {
} else {
var content = sectionContentDict .valueForKey(sectionTitleArray.objectAtIndex(indexPath.section) as! String) as! NSArray
cell.CategoryLabel.text = content .objectAtIndex(indexPath.row) as? String
cell.backgroundColor = UIColor( red: 49/255,green: 46/255,blue:47/255,alpha: 1.0 )
}
return cell
}
func tableView(tableView: UITableView,diddeselectRowAtIndexPath indexPath: NSIndexPath) {
println(indexPath.row)
}
我不知道为什么你需要用你喜欢的部分去做,你可以用正常的细胞来实现,根本不需要复杂化.只需区分子细胞的亲本细胞即可.
我在Github中有一个存储库可以实现你想要的,而不使用UITapGestureRecognizer这两个部分.我会尽快更新项目,以便在下拉列表中获得更好的性能和更深层次的深度.
您可以访问它AccordionMenu
,并随时发布您需要的任何问题.
我希望这对你有帮助.