2016-09-19 5 views
-1

테이블보기가 있습니다. 선택한 테이블보기를 선택한 셀 색으로 변경하고 스크롤 테이블보기를 사용할 때 셀 색이 변경되지 않습니다. 내 코드가 있습니다 :테이블 뷰 저장 선택 행

override func tableView(tableView: UITableView, didSelectRowAtIndexPath 
indexPath: NSIndexPath) { 
    let selectCell = tableView.indexPathForSelectedRow 
     self.selectedCell.append(selectCell!) 
for i in selectedCell 
     { 
      if(!(i .isEqual(indexPath))) 
      { 
       let currentCell = tableView.cellForRowAtIndexPath(i)! as UITableViewCell 

       currentCell.backgroundColor = UIColor.lightGrayColor() 
      } 
     } 

스크롤 테이블보기시 코드가 작동하지 않습니다.

+0

선택시 didSelectRowAtIndexPath에 현재 indexpath.row를 저장할 변수를 만듭니다. 이제 현재 indexpath.row에 대해 cellForRowAtIndexPath 내부를 확인하십시오. 저장된 indexpath.row가 일치하면 색상을 변경하십시오. – Tuhin

+0

선택한 모든 색인 색을 변경해야합니까 ??? –

+0

선택한 모든 행 색상을 변경해야하는 경우 선택한 인덱스를 didSelectRowAtIndexPath 메서드의 배열에 저장하고 cellForRowAtIndexPath 메서드에서 배열에 인덱스 경로가 있는지 여부를 확인하고 그에 따라 처리합니다 –

답변

2

코드가 매우 이상하게 보입니다. 셀을 선택할 때마다 다른 모든 셀의 배경색을 설정할 필요가 없습니다. 이 같은

var selectedCells = [Int: Bool]() 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.backgroundColor = UIColor.lightGrayColor() 
     selectedCells[indexPath.row] = true 
    } 
} 

다음 cellForRowAtIndexPath 방법에있는 backgroundColor를 설정하는 것을 사전을 확인하십시오 :

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

    if selectedCells[indexPath.row] == true { 
     // Color for selected cells 
     cell.backgroundColor = UIColor.lightGrayColor() 
    } else { 
     // Color for not selected cells 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
    //Rest of your Cell setup 

    return cell 
} 
유형 Int:Bool의 사전 그래서 당신은이 같은 각 indexpath.row에 대한 true으로 설정할 수 있습니다로 celectedCells 정의