2016-09-03 1 views
0

뷰로드가 여러 가지 방법을 시도했지만 아무 것도 작동하지 않을 때 테이블 행을 미리 선택하려고합니다. 처음에는 행을 선택하면 텍스트 색상 변경을 정의하기 위해 두 가지 방법을 다음과 같은 설정 한 다음 내가 행을 미리 선택하는 일을 다음을 수행하려고했습니다보기에서 행 선택로드

open func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = menuCells[indexPath.row] 

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(0.4) 
} 

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = menuCells[indexPath.row] 

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(1.0) 

    UIApplication.shared.sendAction(cell.menuAction, to: cell.menuTarget, from: cell, for: nil) 
} 

를 해제하지만 변화하지 않는 것 같습니다했습니다 텍스트를 1.0에서 알파로

open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    if indexPath.row == 0 { 
     cell.setSelected(true, animated: false) 
    } 
} 



menuView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: UITableViewScrollPosition.none) 

답변

2

didSelectRowAt : 메시지

https://developer.apple.com/reference/uikit/uitableview/1614875-selectrowatindexpath


그래서 viewDid 아래의 예처럼 수동으로 didSelectRowAtIndexPath를 호출해야합니다 방법을 나타난다 :

class ViewController: UIViewController { 

    @IBOutlet weak var myTableView: UITableView! 


    override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 


    let indexPath = NSIndexPath(forRow: 2, inSection: 0) 

    myTableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None) 

    // Calling manually to the delegate method 
    tableView(myTableView, didSelectRowAtIndexPath: indexPath) 
    } 

} 


// UITableView Delegate Implementation 
extension ViewController: UITableViewDataSource, UITableViewDelegate { 

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.textLabel?.textColor = UIColor.blackColor() 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.textLabel?.textColor = UIColor.redColor() 
    cell?.textLabel?.backgroundColor = UIColor.clearColor() 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

    cell.textLabel?.text = String(indexPath.row) 

    return cell 
    } 
} 

결과 :

image

1

이게 뭐야?

가있는 tableView (을받을 수있는 대리자를 발생하지 않습니다 selectRowAtIndexPath를 호출 : willSelectRowAt : 나있는 tableView ( : 애플의 문서에 따르면

override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 

     let iPath = NSIndexPath(forRow: 0, inSection: 0) 
     tableView.selectRowAtIndexPath(iPath, animated: true, scrollPosition: .Top) 
    } 
0

tableView(_:didSelectRowAt:)UITableView 인스턴스 사용자 상호 작용을 처리하기위한 대리자 프로토콜의 일부임을 기억하십시오. 따라서 사용자 상호 작용에 대한 응답으로 프로그램 변경이 필요하지 않습니다.

이 문제를 해결하려면 외관 변경을 applyAppearanceSelected과 같은 별도의 함수로 옮기고 프로그래밍 변경을 수행 할 때이를 호출하고 tableView(_:didSelectRowAt:)에서 호출하여 사용자가 시작한 변경 사항을 처리하십시오. 사용자이 셀을 선택할 때만 적용되는 다른 코드를 추가 할 수 있으므로 직접 tableView(_:didSelectRowAt:)에 전화하는 것은 좋지 않습니다.

0

보기가 완전히 초기화되었으므로 viewWillAppear 메서드에서 행을 선택하는 것이 가장 적절하다고 생각합니다.

예는 아래의 첫 번째 섹션의 첫 번째 셀을 선택한다 :

override func viewWillAppear(_ animated: Bool) { 
    let selectedIndexPath = IndexPath(row: 0, section: 0) 
    tableView.selectRow(at: selectedIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.none) 
    tableView(tableView, didSelectRowAt: selectedIndexPath) 
}