How To USe By Default Selecected Row And Checkmark UITableView In Swift

let indexPath = NSIndexPath(forRow: 0, inSection: 0);
    self.tableView.selectRowAtIndexPath(indexPath, animated: false, 
 scrollPosition: UITableViewScrollPosition.None)
    self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
 
func tableView(tableView: UITableView!, didSelectRowAtIndexPath 
indexPath: NSIndexPath!) {
    NSLog("You selected cell number: \(indexPath.row)!")
    self.performSegueWithIdentifier("yourIdentifier", sender: self
}
 
 
 
override func tableView(tableView: UITableView, didSelectRowAtIndexPath
 indexPath: NSIndexPath) {

    let indexPath = tableView.indexPathForSelectedRow()

    let currentCell = tableView.cellForRowAtIndexPath(indexPath) 
 as UITableViewCell

    println(currentCell.textLabel!.text)  
 
 
 
override func viewDidLoad() {
    super.viewDidLoad()

    // …

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:
  "categoryCell")
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: 
 NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("categoryCell",
  forIndexPath: indexPath) as UITableViewCell
    cell.accessoryType = (lastSelectedIndexPath?.row == indexPath.row)
  ? .Checkmark : .None
    cell.textLabel?.text = categories[indexPath.row]

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: 
 NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if indexPath.row != lastSelectedIndexPath?.row {
        if let lastSelectedIndexPath = lastSelectedIndexPath {
     let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
            oldCell?.accessoryType = .None
        }

        let newCell = tableView.cellForRowAtIndexPath(indexPath)
        newCell?.accessoryType = .Checkmark

        lastSelectedIndexPath = indexPath
    }
} 

Popular posts from this blog

How to Use pagination ScrollView in Swift

UISearchBar search text color , background color swift 3

How To Add Multiple Line in Lable in Swift Ios