ios - 테이블뷰 - uitableview example swift 4
신속하게 터치 또는 클릭 된 tableView 셀을 감지하는 방법 (6)
매번 망쳐!
tableView
델리게이트 및
dataSource
가
viewDidLoad
선언되어 있는지 확인하십시오.
그런 다음 일반적으로 몇 가지 배열을 채워 반환 된 데이터를 시뮬레이션 한 다음 거기에서 가져옵니다!
//******** 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!
}
TableView
에서 선택한 항목의
index
을 가져오고 그 후 일부 활동을 시작하려고합니다.
불행히도 내가 찾은 대부분의 솔루션은 objective-c에 있거나 작동하지 않습니다.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
메소드는
cell
레이블을 인쇄하지 않습니다.
누군가 나를 도와 줄 수 있습니까?
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() } }
몇 번의 시도 후에 코드를 내가 찾은 자습서와 다른 코드로 변경했습니다. 그리고 그것은 작동하지 않습니다. 이제 이것이 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])!") } }
발생해야하는 몇 가지 사항 ...
-
뷰 컨트롤러는
UITableViewDelegate
유형을 확장해야합니다. -
뷰 컨트롤러에는
didSelectRowAt
함수가 포함되어야합니다. -
테이블 뷰에는 뷰 컨트롤러가 대리자로 할당되어 있어야합니다.
다음은 델리게이트 할당이 가능한 곳입니다 (뷰 컨트롤러 내).
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
view = tableView
}
didSelectRowAt
함수의 간단한 구현입니다.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row: \(indexPath.row)")
}
이것은 나를 위해 잘 작동했습니다 :
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
출력은 다음과 같아야합니다.
section: 0
row: 0
테이블 뷰 대리자와 데이터 소스를 상속하십시오. 필요한 것을 위임하십시오.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
마지막 으로이 대리자를 구현하십시오.
func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
print("row selected : \(indexPath.row)")
}
tableView 셀의 Array에서 요소를 신속하게 터치하거나 클릭하려면
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)")
}
스위프트 3.0
델리게이트 메소드를 통해 tableview 셀의 터치 / 클릭 이벤트를 찾을 수 있습니다. 또한 이와 같이 셀의 섹션 및 행 값을 찾을 수 있습니다.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}