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 dataSourc
e 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
}
}
Hope this helps
Related Topics
Left-Align Image and Center Text on Uibutton
Watch Os 2.0 Beta: Access Heart Beat Rate
Ios5 What Does "Discarding Message for Event 0 Because of Too Many Unprocessed Messages" Mean
Why Does Uicollectionview Log an Error When the Cells Are Fullscreen
Adding Frameworks to Project in Xcode 5 and Having *Relative* Paths Added
Swift - Coredata Migration - Set New Attribute Value According to Old Attribute Value
How to Create and Save Ekcalendar on iOS 6
Hide Navigationbar When Scrolling Tableview in Collectionview
Detect If a User Has Typed an Emoji Character in Uitextview
Change the Uitextview Text Direction
Memory Leak with Large Core Data Batch Insert in Swift
How to Find the Purple Port for the Front Most Application in iOS 5 and Above
Avqueueplayer Playback Without Gap and Freeze
Detect If Swift App Is Being Run from Xcode