-1

the cell is very simple, just a textLabel. i want to tap the cell and then print some words. this is a test . but the didSelectRowAtIndexPath is not work. Please tell me why, thank you !

import UIKit
import SnapKit

class ListTableViewController : UITableViewController{
    var itemData: ListTableModel!
    var header: UIView!
    override init(style: UITableViewStyle){
        super.init(style: style)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        itemData = ListTableModel()
        self.tableView.dataSource = self
        self.tableView.delegate = self


    }


    ....


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("mCell", forIndexPath: indexPath)
        cell.textLabel?.userInteractionEnabled = false
        let listCell = cell as! ListTableViewCell
        if let item = itemData.getItemData(indexPath.row) {
            listCell.textLabel!.text = item.valueForKey("title") as? String
        }

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("indexPathS")
    }

}
carly
  • 11
  • 3
  • Looks duplicate. Refer this answer: http://stackoverflow.com/a/12855662/3051458 – Ramesh_T Jan 07 '16 at 06:50
  • class ListTableViewController : UITableViewController , UITableViewDelegate{...} it will show error: Redundant conformance of "ListTableViewController" to protocol "UITableViewDelegate". – carly Jan 07 '16 at 06:56

2 Answers2

2

Please check if self.tableView.allowsSelection = NO is not written anywhere. And in the storyboard, check if single selection is checked under Selection tab.

nr5
  • 4,228
  • 8
  • 42
  • 82
  • check in XIB file then ? – nr5 Jan 07 '16 at 07:37
  • there is no "self.tableView.allowsSelection = NO" in all files – carly Jan 07 '16 at 08:36
  • are you overriding the touch gestures in any way in your custom cell class? – nr5 Jan 07 '16 at 09:12
  • no. the cell is very simple: import UIKit class ListTableViewCell: UITableViewCell{ required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override init(style:UITableViewCellStyle, reuseIdentifier: String?){ super.init(style: style, reuseIdentifier:reuseIdentifier) } } – carly Jan 08 '16 at 02:08
0
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let text:NSString = myArray .objectAtIndex(indexPath.row) as! NSString
        delegate?.myMethod(text as String)
        self.navigationController?.popToRootViewControllerAnimated(true)

    }

you can try this.

Swift Developer
  • 247
  • 2
  • 18