Ik probeer index
van het geselecteerde item in tableView
te krijgen en daarna wat activiteit te starten. Helaas zijn de meeste oplossingen die ik heb gevonden in objective-c of werken ze niet.
Methode func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
druk het cell
label niet af..
Kan iemand me alsjeblieft helpen?
import UIKit
import ResearchKit
class TaskListViewController: UIViewController, UITableViewDataSource {
let tasks=[("Short walk"),
("Audiometry"),
("Finger tapping"),
("Reaction time"),
("Spatial span memory")
]
//how many sections are in your table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//return int how many rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
//what are the contents
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
var (testName) = tasks[indexPath.row]
cell.textLabel?.text=testName
return cell
}
// give each table section a name
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Tasks"
}
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()
}
}
Na een paar pogingen heb ik de code gewijzigd in een andere dan de tutorial die ik heb gevonden. En het werkt ook niet. Nu denk ik dat dit het probleem is met de iOS-simulator…
import UIKit
import ResearchKit
class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet
var tableView: UITableView?
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(items[indexPath.row])!")
}
}
Antwoord 1, autoriteit 100%
Als u de waarde van de cel wilt, hoeft u de cel niet opnieuw te maken in het didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(tasks[indexPath.row])
}
Taak zou als volgt zijn:
let tasks=["Short walk",
"Audiometry",
"Finger tapping",
"Reaction time",
"Spatial span memory"
]
u moet ook de cellForRowAtIndexPath
controleren, u moet de identifier instellen.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
var (testName) = tasks[indexPath.row]
cell.textLabel?.text=testName
return cell
}
Hopelijk helpt het.
Antwoord 2, autoriteit 50%
In Swift 3.0
Je kunt de gebeurtenis voor de aanraking/klik van de cel van tabelweergave vinden via de gedelegeerde methode. Kan ook de sectie- en rijwaarde van de cel op deze manier vinden.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
Antwoord 3, autoriteit 18%
Een paar dingen die moeten gebeuren…
-
De view-controller moet het type
UITableViewDelegate
-
De view-controller moet de functie
didSelectRowAt
bevatten. -
Aan de tabelweergave moet de weergavecontroller zijn toegewezen als gemachtigde.
uitbreiden
Hieronder is een plaats waar het toewijzen van de gedelegeerde zou kunnen plaatsvinden (binnen de view controller).
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
view = tableView
}
En een eenvoudige implementatie van de functie didSelectRowAt
.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row: \(indexPath.row)")
}
Antwoord 4, autoriteit 12%
Het probleem is door mijzelf opgelost met behulp van de tutorial van weheartswift
Antwoord 5, autoriteit 9%
Dit werkte goed voor mij:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
Antwoord 6, autoriteit 8%
Erf de tableview-gemachtigde en gegevensbron.
Implementeer afgevaardigden wat u nodig heeft.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
En tot slot deze afgevaardigde implementeren
func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
print("row selected : \(indexPath.row)")
}
Antwoord 7, autoriteit 5%
Om een element uit Array in tabelweergave te krijgen, wordt de cel snel aangeraakt of aangeklikt
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text= arr_AsianCountries[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexpath = arr_AsianCountries[indexPath.row]
print("indexpath:\(indexpath)")
}
Antwoord 8, autoriteit 3%
# Check delegate? first must be connected owner of view controller
# Simple implementation of the didSelectRowAt function.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("row selection: \(indexPath.row)")
}
Antwoord 9, autoriteit 2%
Ik verpest het elke keer weer! Zorg ervoor dat de tableView
gedelegeerde en dataSource
gedeclareerd zijn in viewDidLoad
. Dan vul ik normaal gesproken een paar arrays in om geretourneerde gegevens te simuleren en neem het dan vanaf daar!
//******** Populate Table with data ***********
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? SetupCellView
cell?.ControllerLbl.text = ViewContHeading[indexPath.row]
cell?.DetailLbl.text = ViewContDetail[indexPath.row]
cell?.StartupImageImg.image = UIImage(named: ViewContImages[indexPath.row])
return cell!
}
Antwoord 10
U moet de didSelect-functie implementeren.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("User touched on \(indexpath) row")
}