2017-03-07 8 views
-1

저는 IOS 개발에 절대적으로 새로운 것으로,이 문제에 집착했습니다. 나는 여기에서 아주 간단한 것을 시도하고 있는데, 나는 그 셀을 선택할 때 그 배경색이 회색으로 바뀌고 다른 셀을 선택하면 이전 회색 셀의 배경색이 다시 기본 색으로 복원되어야한다고 생각한다. 내가 다음 코드를 사용하고이를 달성하기 위해 : -치명적인 오류 : didDeselectRowAt의 선택 값을 언 래핑하는 동안 예기치 않게 nil이 발견되었습니다.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{  
    let currentCell = tableView.cellForRow(at: indexPath) as! RoundTableViewCell 
    currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3") 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell // Error 
    currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
} 

은 위의 코드는 잘 작동하지만 이전에 선택한 셀이 같은 테이블 뷰에 표시되지 동안 나는 새로운 셀을 누르려고 할 때 충돌 멀리 스크롤됩니다. 여기서 일어나는 일을 이해할 수 없습니다. 어떤 도움을 주시면 감사하겠습니다. 감사.

편집

cellForRowAt는 :

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell: RoundTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "cell")! as? RoundTableViewCell)! 
    cell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffff  
    return cell 
} 
+0

'cellForRow' 메소드 구현을 표시 할 수 있습니까? 그건 그렇고, 당신은'할 필요가 없습니다! 귀하의 경우 RoundTableViewCell' 그냥 제거하고 모든 괜찮을 것입니다. –

+0

알았습니다. 알았습니다. –

+1

설명서를 참조하십시오. https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow : 표 셀을 나타내는 객체이거나 셀이 표시되지 않는 경우 ** ** ** –

답변

2

당신은 휴대 가능 여부

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
    } 
} 
+0

와우 !! 덕분에 지금은 작동합니다. –

1
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    .... 
    .... 
    if cell.isSelected == true { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3")  
    } 
    else { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff") 
    } 
    .... 
    .... 
} 

는 여러 셀 선택을 원하는 경우에 있는지 확인해야합니다.

table.allowsMultipleSelection = true 

응용 프로그램 충돌이 전무 가치를 얻을 수 있도록 세포가 메모리에서 화면에서의 할당 해제를 사라질 때 때문이다. 그래서 이것을 시도하십시오

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{  
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3")  
    } 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
    } 
} 
0

예상 결과를 얻으려면 다음 코드를 사용하십시오.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 


     cell.selectedBackgroundView = UIImageView() 
     cell.backgroundView=UIImageView() 

     let selectedBackground : UIImageView = cell.selectedBackgroundView as! UIImageView 
     selectedBackground.image = UIImage.init(named:"selected.png"); 

     let backGround : UIImageView = cell.backgroundView as! UIImageView 
     backGround.image = UIImage.init(named:"defaultimage.png"); 

     return cell 


    }