Skip to content

Instantly share code, notes, and snippets.

@tkc tkc/CustomeCellDelegate
Last active Aug 21, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.