2016-10-14 9 views
0

나는 TableViewController에 정적 테이블이 있습니다. 행을 선택할 때마다 셀 전체가 회색으로 채워집니다. 내가 탭하는 모든 행에 대해이 작업을 수행합니다. 내가 사용하는 경우 :선택 세포가 제대로

cell.selectionStyle = .none에게

을이 셀 대신 흰색 채우기 할 것입니다.

의 tableview 속성 관리자 :

TableView Attributes Inspector

TableViewCell 검사기 특성 :

TableViewCell Attributes Inspector

TableViewController :

import UIKit 

class OptionTableViewController: UITableViewController { 

@IBOutlet var optionsTable: UITableView! 

let defaults = UserDefaults.standard 

let numberOfRows = [7,2] 

let cellIdentifier = "OptionCells" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    optionsTable.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that con be recreated. 
} 
override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 2 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    var rows = 0 
    if(section < numberOfRows.count){ 
     rows = numberOfRows[section] 
    } 
    return rows 
} 

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.selectionStyle = .none 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 

    cell.textLabel?.text = optionSelections[indexPath.row] 
    return cell 
} 
} 

나는 내가 제대로 모든 설정을 믿습니다. 왜 내가 세포를 두 드릴 때 세포를 채우는가?

업데이트 : 현재 가지고있는 것을 보여주기 위해 코드가 업데이트되었습니다.

업데이트 2 : 다른 모든 셀을 탭하기 전후의 테이블 모양을 보여주기 위해 두 장의 그림을 포함했습니다.

업데이트 3 : 셀을 dequeuing하여 액세서리 유형을 체크 표시로 변경했습니다.

는 전에 : Before Tapping Cells

후 : After Tapping Every Other Cell

+0

; 셀이 재사용되고 셀 중 일부가 선택되어 (아마도) 셀렉션의 selectionStyle이 이미 '있기 때문에 일관성이없는 이유는 무엇입니까?없음 '을 표시합니다. – holex

+0

@ 홀렉스 그게 실수 였어.하지만 selectionStyle을 올바른 위치에 놓았을 때도 여전히 셀을 회색으로 채 웁니다. – user1715916

+0

didSelectRowAt 메서드를 제거해보십시오. 셀을 큐에서 꺼내므로 안됩니다. 나는이 세포들이 "올바른"세포의 꼭대기에 쌓여 있다고 의심한다. 보기 디버거를 사용하여 확인하십시오. – pbasdf

답변

1

, 나는 문제가 didSelectRow(at:) 방법 대기열에서 세포의 증상이라고 생각 : 셀을 가져옵니다

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 
} 

대신

let cell = tableView.cellForRow(at: indexPath) 

를 사용해야하는 현재 지정된 indexPath에 있습니다 (indexPath가 스크롤되지 않았거나 화면에 표시되지 않은 경우 nil을 반환 할 수 있습니다). Dequeuing은 사용하지 않는 셀을 주어진 indexPath로 이동시킵니다. 그러면 기존 셀 앞에 위치합니다. 따라서 이상한 동작이 발생합니다. 선택한 셀이 정말 똑똑하지 _after_은`selectionStyle`을 변경

1

당신은 선택이 발생하기 전에 selectionStyle를 설정해야합니다. 주석에서 설명하고있는 바와 같이

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.selectionStyle = .none 
    cell.selectedBackgroundView = UIView() // optional 
    cell.selectedBackgroundView?.backgroundColor = UIColor.white // optional 
} 
+0

나는 이것을 시도했지만 이제는 내 레이블을 볼 수 없으며 여전히 전체 셀을 회색으로 채 웁니다. – user1715916

+0

나는 이것을 시도했을 때 didSelectRowAt에서 cell.selectionStyle = .none 라인을 삭제했다는 것을 추가해야한다. – user1715916

+0

아마도 정적 셀을 사용할 때이 사용자 정의를 방해하는 무언가가있을 수 있습니다. 위의 코드는 동적 셀에서 확실히 작동합니다. 레이블 문자열을 정적 배열로 유지하고 cellForRowAt에 설정할 수 있습니까? –