2017-01-16 1 views
0

city Tab 아래에서 표 행을 선택하고 areaOne에있을 때 Ok을 강조 표시하려면 city 아래의 동일한 표 행을 areaTwo button으로 변경하더라도 강조 표시된 상태로 유지됩니다. 어떻게 이런 일이 일어나지 않도록합니까?버튼을 눌렀을 때 행을 선택하는 방법은 무엇입니까?

var citySelectedIndexPaths = [IndexPath]() 
var townSelectedIndexPaths = [IndexPath]() 
@IBOutlet weak var areaOne: UIButton! 
@IBOutlet weak var areaTwo: UIButton! 
var popValue:Int! 

@IBAction func areaSelect(_ sender: UIButton) { 
    let areas: [[UIButton]] = [[areaOne],[areaTwo]] 
    switch sender { 
    case areaOne: 
     popValue = 1 
    case areaTwo: 
     popValue = 2 
    default: break 
    } 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell 
    //resetting the color 
    myCell.backgroundColor = UIColor(white: 1.0, alpha:1.0) 
    switch (segCtrl.selectedSegmentIndex) 
    { 
    case 0: 
     myCell.myLabel.text = cityName[indexPath.row] 

//I like to know how to add condition for button selected below 
// so as to display popValue =1 when areaOne is tapped 
//instead of me doing it manually like below 

if citySelectedIndexPaths.contains(indexPath) && (popValue == 1) { 
      myCell.backgroundColor = UIColor(white: 1.0, alpha:0.5) 
     } 
     break 
    case 1: 
     myCell.myLabel.text = townName[indexPath.row] 
     if townSelectedIndexPaths.contains(indexPath) { 
      myCell.backgroundColor = UIColor(white: 1.0, alpha:0.5) 
     } 
     break 
    default:break 
    } 
    return myCell 
} 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 
    let cell = tableView.cellForRow(at: indexPath)! 
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert) 

let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in 
    let cell = tableView.cellForRow(at: indexPath)! 
    cell.backgroundColor = UIColor(white: 1.0, alpha:0.5) 
    switch (segCtrl.selectedSegmentIndex) 
    { 
    case 0: 
     if citySelectedIndexPaths.contains(indexPath) { 
      let indexValue = citySelectedIndexPaths.indexOf(indexPath) 
      citySelectedIndexPaths.removeAtIndex(indexValue) 
     } else { 
      citySelectedIndexPaths.append(indexPath) 
     } 
     break 
    case 1: 
     if townSelectedIndexPaths.contains(indexPath) { 
      let indexValue = townSelectedIndexPaths.indexOf(indexPath) 
      townSelectedIndexPaths.removeAtIndex(indexValue) 
     } else { 
      townSelectedIndexPaths.append(indexPath) 
     } 
     break 
    default:break 
    } 
}) 
alertController.addAction(ok) 
present(alertController, animated: true, completion: nil) 
} 
+0

당신이 에어리어 원하고 areaTwo이 무엇인지에 대한 컨텍스트를 제공 할 수 있습니다. 어쩌면 스크린 샷을 제공 할 수 있습니까? – JoGoFo

+0

당신의 논리에서 이제는'cell.backgroundColor = UIColor (white : 1.0, alpha : 0.5)'만 보았고, 이전에 선택된 셀에 대해'cell.backgroundColor'를 리셋하기위한 스 니펫을 보지 않았습니다. – nynohu

답변

0

UITableviewCell의 선택/선택 해제 후에 항상 발생하는 두 가지 대리자 방법이 있습니다. 아래의 방법을 참고하십시오. 내 경우 toggleCheckBox()에 여기

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     let deSelectedCell = tableView.cellForRow(at: indexPath) as? UITableviewCell 
     deSelectedCell?.toggleCheckBox()   
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let selectedCell = tableView.cellForRow(at: indexPath) as? UITableviewCell 
     selectedCell?.toggleCheckBox() 
} 

은 셀 선택에 따라 체크 박스의 선택/선택 취소를 결정하는 기능입니다/선택 취소입니다 -
// Called after the user changes the selection. 
@available(iOS 2.0, *) 
optional public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) //Called after selection of cell 

@available(iOS 3.0, *) 
optional public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) //Called after deselection of cell. 

지금 당신의 점은 버튼 선택에 따라 변경하는 것입니다 세포 자체 내부.

내부에서 셀 선택을 업데이트하는 대신 cellForRowAt indexPath이 대리자 내부에서 업데이트를 시도하십시오. 귀하의 질문을 보면서 어떤 상황을 처리하려고하는지 명확하게 알 수는 없지만 선택 및 선택 취소에 따라이 위임자에 대한 기본 tableView 데이터를 업데이트하고 필요에 따라 전체 테이블을 업데이트 할 수 있습니다.

EDIT 지금 당신의 필요 조건에 따라 당신이있는 tableView 데이터 객체의 배열을 만들 수 있습니다. 다음과 같이 -

private var orderDetailsTableViewData: [[String: Any]] = [["cellSelectionColor": UIColor.blue], ["cellSelectionColor": UIColor.white]] 

선택 사항에 따라 코드를 업데이트하고 요구 사항에 따라 tableView를 업데이트하면됩니다.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
       let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! UITableviewCell() 
if let cellColor = orderDetailsTableViewData[indexPath.row]["cellSelectionColor"] as? UIColor{ 
    cell.backgroundColor = cellColor 
}else{ 
    cell.backgroundColor = UIColor.clear 
} 
    return cell 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    orderDetailsTableViewData[indexPath.row].updateValue(UIColor.blue, forKey: "cellSelectionColor") 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    orderDetailsTableViewData[indexPath.row].updateValue(UIColor.white, forKey: "cellSelectionColor") 
} 

이제는 요구 사항에 따라 테이블을 업데이트 할 수 있습니다. BTW 이것은 세포의 선택을 유지할 수있는 방법에 대한 대략적인 아이디어입니다. 보다 중요한 작업을 위해 필요에 따라 사용자 지정할 수 있습니다.

희망이 도움이되었다. :)

+0

나는 내 질문을 업데이트했다. 더 명확히하기를 바란다. 나는 도시와 마을의 목록을 가지고있다. 나는 눌러 진 버튼에 따라 강조된 도시가 남아 있기를 바랐다. – leaner122

+0

업데이트를 확인하십시오. :) – Tuhin

관련 문제