Get Selected Index of Uitableview

Get Selected index of UITableView

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

How can I get selected item in UITableView

First you have to set @IBOutlet to the tableView in your ViewController and set as it's delegate and dataSource to you can see the data an respond to changes in the tableView ;

class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.delegate = self
self.tableView.dataSource = self
}

After

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}

You can get selected cell.
Ty.

Get the selected cell number in a page

You just have to apply the modulo operator:

let indexOnThePage = indexPath.row % 3

indexOnThePage will be 0 for the first, 1 for the second, and 2 for the third.

4 % 3 == 1 // That means the second cell of the page

Bonus: If you want the page number, just do:

let pageNumber = indexPath.row / 3

It'll give 0 on the first page, 1 on the second, and so on.

EDIT: Here is the the version with you last page problem.

func indexOnPage(row: Int, page: Int, total: Int, itemsOnPage: Int = 3) -> Int {
let lastPage = total / itemsOnPage

if page != lastPage {
return row % itemsOnPage
}
else {
let delta = total % itemsOnPage
return (row - delta) % itemsOnPage
}
}

indexOnPage(row: 0, page: 0, total: 5) // 0
indexOnPage(row: 1, page: 0, total: 5) // 1
indexOnPage(row: 2, page: 0, total: 5) // 2
indexOnPage(row: 2, page: 1, total: 5) // 0
indexOnPage(row: 3, page: 1, total: 5) // 1
indexOnPage(row: 4, page: 1, total: 5) // 2

This should work for your problem.

How to set selected some index of TableView

First select multipleSelection checkmark in storyboard, or by code self.testTableView.allowsMultipleSelection = true, then after tableview.reloadData put this 2 lines

    self.testTableView.selectRow(at:  IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)

Standard Code

    self.testTableView.reloadData()
self.testTableView.allowsMultipleSelection = true
//here is where selection is made
self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)

Specified this Example Code

var variable = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

import UIKit

class ControllerClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var testTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return variable.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell
cell.accessoryType = .none
cell.lblWordText?.text = variable[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

}

override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = true
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
self.testTableView.allowsMultipleSelection = true
self.testTableView.reloadData()
//here is where selection is made
self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
}
override func viewDidLoad() {
testTableView.dataSource = self

}
}

Sample Image

Hope this helps



Related Topics



Leave a reply



Submit