我是iOS的初学者,使用swift 2。
我基于这个视频:
https://www.youtube.com/watch?v=itbok1NZvss
(基于SWIFT 3)创建可扩展的UITable
。但点击时UITableViewCell
不展开。在这种情况下,UITapGestureRecognizer
不起作用。
我的代码:
struct Section {
var genre: String!
var movies: [String]!
var expanded: Bool!
init(genre: String , movies: [String] , expanded: Bool)
{
self.expanded = expanded
self.genre = genre
self.movies = movies
} }
ExpandableHeaderView类:
protocol ExpandableHeaderViewDelegate
{
func toggleSection(header: ExpandableHeaderView , section: Int)
}
class ExpandableHeaderView: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var section: Int!
override init(reuseIdentifier: String?)
{
super.init(reuseIdentifier: reuseIdentifier)
//self.userInteractionEnabled = true
self.addGestureRecognizer(UIGestureRecognizer(target: self , action: Selector("selectHeaderAction:")))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer)
{
let cell = gestureRecognizer.view as! ExpandableHeaderView
delegate?.toggleSection(self , section: cell.section)
print("selectHeaderAction")
}
func customeInit(title: String, section: Int , delegate: ExpandableHeaderViewDelegate)
{
self.textLabel?.text = title
self.section = section
self.delegate = delegate
}
override func layoutSubviews()
{
super.layoutSubviews()
self.textLabel?.textColor = UIColor.whiteColor()
self.contentView.backgroundColor = UIColor.darkGrayColor()
}
}
的ViewController:
class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource , ExpandableHeaderViewDelegate{
@IBOutlet weak var tableView: UITableView!
var sections = [
Section(genre: "1" , movies: ["dfgdgd" , "dgdgdg" , "jsgdhgdg"] , expanded: true ) ,
Section(genre: "2" , movies: ["dfgdgd" , "dgdgdg"] , expanded: false ) ,
Section(genre: "3" , movies: ["dfgdgd" , "dgdgdg"] , expanded: true )
]
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 sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].movies.count
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(sections[indexPath.section].expanded == true)
{
return 44
}
else
{
return 0
}
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customeInit(sections[section].genre, section: section, delegate: self)
return header
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("labelCell")
cell!.textLabel?.text = sections[indexPath.section].movies[indexPath.row]
return cell!
}
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0 ..< sections[section].movies.count
{
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i, inSection: section)], withRowAnimation: UITableViewRowAnimation.Automatic)
}
tableView.endUpdates()
}
}
答案 0 :(得分:2)
您需要指定所需的手势类型,在这里您必须添加UITapGestureRecognizer
:
self.addGestureRecognizer(UITapGestureRecognizer(target: self , action: Selector("selectHeaderAction:")))
答案 1 :(得分:0)
试试这个。对我来说工作正常。 你只需要在你添加点击手势的地方传递目标。
override func viewDidLoad() {
self.addTapGesture(tapNumber: 1, target: self.view, action: #selector(handlePan))
}
func addTapGesture(tapNumber : Int, target: Any , action : Selector) {
let tap = UITapGestureRecognizer(target: target, action: action)
tap.numberOfTapsRequired = tapNumber
(target as AnyObject).addGestureRecognizer(tap)
}
@objc func handlePan() {
print("call tap gesture event")
}