2015-07-02 12 views
21

Próbuję uzyskać index wybranego elementu w TableView i rozpocząć później trochę aktywności. Niestety większość rozwiązań, które znalazłem, jest obiektywnych lub nie działa.Jak wykryć komórkę tableView dotkniętą lub klikniętą w szybkim tempie

Metoda func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) nie wydrukować etykietę cell ..

Czy ktoś mógłby mi pomóc proszę?

 
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() 

    } 
} 

Po kilku próbach zmieniłem kod na inny z samouczka, który znalazłem. I to też nie działa. Teraz myślę, że to jest problem z symulatora iOS ...

 
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])!") 
    } 

} 

Odpowiedz

28

Jeśli chcesz wartość z komórki wtedy nie trzeba odtworzyć komórkę w didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    println(tasks[indexPath.row]) 
} 

zadaniem byłoby się następująco:

let tasks=["Short walk", 
    "Audiometry", 
    "Finger tapping", 
    "Reaction time", 
    "Spatial span memory" 
] 

również trzeba sprawdzić cellForRowAtIndexPath trzeba ustawić identyfikator.

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 
} 

Mam nadzieję, że to pomaga.

+0

@pawisoon Myślę, że masz problem w cellForRow. sprawdź zaktualizowaną odpowiedź –

2

Problem został rozwiązany przez siebie za pomocą tutorial weheartswift

enter image description here

+3

Czy wybrana odpowiedź rzeczywiście rozwiązuje problem? Jeśli to nie rozwiąże problemu, napisz rozwiązanie do swojej odpowiedzi lub wybierz odpowiednią odpowiedź, aby naprawić pytanie, które zadałeś. To, że ktoś oferuje pomoc i możliwe rozwiązanie, nie oznacza, że ​​odpowiedź powinna być wybrana jako właściwa. Napraw to. – Devbot10

17

w Swift 3.0

można znaleźć imprezę dla touch/kliknięciem komórce tableview przez to przekazać metodę . Ponadto można znaleźć wartość przekroju i wiersza komórki w ten sposób.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("section: \(indexPath.section)") 
     print("row: \(indexPath.row)") 
} 
1

Ten pracował dobrze dla mnie:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
 
     print("section: \(indexPath.section)") 
 
     print("row: \(indexPath.row)") 
 
    }

Wyjście powinno być:

section: 0 
row: 0 
+3

Twoja odpowiedź wydaje się być zduplikowana, ponieważ ta odpowiedź jest bardzo podobna do powyższej odpowiedzi https://stackoverflow.com/a/41583381/40867 – jdev

0

A kilka rzeczy, które muszą się wydarzyć ...

  1. Widok kontroler musi rozszerzania rodzaju UITableViewDelegate

  2. Sterownik widok musi zawierać funkcję didSelectRowAt.

  3. Widok tabeli musi mieć kontroler widoku przypisany jako delegat.


Poniżej jest jedno miejsce, gdzie przypisywanie delegata mogłoby nastąpić (w widoku kontrolera).

override func loadView() { 
    tableView.dataSource = self 
    tableView.delegate = self 
    view = tableView 
} 

I prosta implementacja funkcji didSelectRowAt.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("row: \(indexPath.row)") 
} 
Powiązane problemy