Skip to content

Instantly share code, notes, and snippets.

@tkc
Last active August 21, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkc/035e16f0cc194b1014e63f865d1c8c58 to your computer and use it in GitHub Desktop.
Save tkc/035e16f0cc194b1014e63f865d1c8c58 to your computer and use it in GitHub Desktop.
protocol TouchCellDelegate {
func getNo(id: Int) -> Void
}
class CustomeCell: UITableViewCell
{
var id:Int?
var delegate: TouchCellDelegate?
var titleLabel = UILabel();
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
titleLabel = UILabel(frame: CGRectMake(10, 2, 300, 15));
titleLabel.text = "";
titleLabel.font = UIFont.systemFontOfSize(12)
self.addSubview(titleLabel);
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CustomeCell.handleTap(_:)))
titleLabel.addGestureRecognizer(gestureRecognizer)
self.bringSubviewToFront(titleLabel);
titleLabel.userInteractionEnabled = true
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
self.delegate?.getNo(self.id!)
}
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)!
}
}
class MainView: UIViewController, UITableViewDelegate, UITableViewDataSource,TouchCellDelegate {
private var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.prepareTable()
}
func prepareTable(){
tableView = UITableView(frame: CGRect(x: 0, y:0, width: self.view.frame.width, height:self.view.frame.height))
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(CustomeCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func getNo(id:Int) -> Void {
print(id)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomeCell
cell.delegate = self
cell.id = indexPath.row
cell.titleLabel.text="demo title"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment