2017-01-01 2 views
0

저는 신속하게 사용하기가 매우 쉽지만 대부분을 따라 할 수있었습니다. 그러나 코드 자체가 코드를 식별 할 수 없다는 오류가 있습니다 (오류가 발생하지 않음). 다음 페이지로 이동하려면 테이블보기에서 행을 클릭하려고하지만 코드를 didselectrow 메서드를 인식하는 생각하지 않습니다. 어쩌면 당신 중 하나가 더 많은 경험이있는 사람이 나를 도울 수 있습니다. 여기 Swift 3에서 "didSelectRowAt indexPath"문제가 있습니다.

코드입니다 :

import UIKit 
import Foundation 

class OnePercentersViewController: UIViewController, UITableViewDataSource 
{ 

    var copiedExecutiveArray : [String] = NSArray() as! [String] 
    var identities : [String] = NSArray() as! [String] 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     let copyExecutiveNames : ExecutiveArray = ExecutiveArray() 
     copiedExecutiveArray = copyExecutiveNames.executiveNames 
     identities = copyExecutiveNames.executiveNames 
    } 

    //how many sections in table 
    func numberOfSections(in tableView: UITableView) -> Int 
    { 
     return 1 
    } 

    //returns int (how many rows) 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return copiedExecutiveArray.count 
    } 

    //contents of each cell 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")//UITableViewCell() 
     let personName = copiedExecutiveArray[indexPath.row] 
     cell?.textLabel?.text = personName 
     return cell! 

    } 

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

     let execName = identities[indexPath.row] 
     let viewController = storyboard?.instantiateViewController(withIdentifier: execName) 
     self.navigationController?.pushViewController(viewController!, animated: true) 
     print("button clicked") 
     tableView.deselectRow(at: indexPath as IndexPath, animated: true) 
    } 

} 
+1

하지'관련 그러나 VAR의 정체성 : 같은 [문자열] = NSArray를()! [String]'은 끔찍합니다. 간단히'var identities = [String]()'라고 쓰십시오. – vadian

답변

0

내가 코드의 다른 부분을 확인하지 않은,하지만 tableView(_:didSelectRowAt:) 작품을 만들기 위해, 당신의 ViewController는 UITableViewDelegate을 준수해야합니다 :

class OnePercentersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

또한 당신이 필요로하는 UITableViewdelegate을 스토리 보드의 ViewController에 연결하십시오. 아니면 코드에서 @IBOutletUITableView 인 경우이를 수행 할 수 있습니다. 어쨌든 dataSource뿐만 아니라 delegate도 있습니다.


그리고 이것은 중요한 문제가되지 않습니다,하지만 당신은 같은 클래스의 처음 두 줄 쓸 수 있습니다 : 비슷한에 주석을 하루 반 지출 유튜브 비디오를보고 읽은 후

var copiedExecutiveArray : [String] = [] 
var identities : [String] = [] 
+0

고마워, 나는 그것을 시험해 보겠다. – Weirdenomics

0

을 문제는 tt가 시행 착오를 겪었지만 마침내 당신 덕분에 나왔습니다! 스토리 보드를 통해 ViewController에 대리인을 연결해야만한다는 것을 읽었을 때 마침내 클릭했습니다.

여기에 유사한 문제를 가진 사람들을위한 최종 코드는 다음과 같습니다

import UIKit 
    import Foundation 

    class OnePercentersViewController: UIViewController,UITableViewDataSource, UITableViewDelegate 
    { 

var copiedExecutiveArray : [String] = [] 
var identities : [String] = [] 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    let copyExecutiveNames : ExecutiveArray = ExecutiveArray() 
    copiedExecutiveArray = copyExecutiveNames.executiveNames 
    identities = copyExecutiveNames.executiveNames 
} 

//how many sections in table 
func numberOfSections(in tableView: UITableView) -> Int 
{ 
    return 1 
} 

//returns int (how many rows) 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return copiedExecutiveArray.count 
} 

//contents of each cell 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    let personName = copiedExecutiveArray[indexPath.row] 
    cell.textLabel?.text = personName 
    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    let execName = identities[indexPath.row] 
    let viewController = storyboard?.instantiateViewController(withIdentifier: execName) 
    self.navigationController?.pushViewController(viewController!, animated: true) 
    tableView.deselectRow(at: indexPath as IndexPath, animated: true) 
} 



    } 
+0

스토리 보드를 통해 대리인을 어떻게 연결할 수 있습니까? :( – JCarlos