2016-09-28 4 views
0

API 호출로 채워진 배열이 있습니다. pointsButton에서스크롤 할 때 UITableView에 단추 상태 유지

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

     cell.cellText = morningArray[indexPath.row].name 
     cell.cellImage = (DosageTypes(rawValue: morningArray[indexPath.row].measurement)?.image)! 
     cell.cellDossage = "\(morningArray[indexPath.row].dosage) \(DosageTypes(rawValue: morningArray[indexPath.row].measurement)!.description)" 
     cell.pointsButton.medication = morningArray[indexPath.row] 
     return cell 
} 

를 I 배열의 약물 객체 통과 셀 : I과 같은 테이블 뷰를 채우는이 배열을 사용한다. pointsButton은 객체의 상태에 따라 상태를 변경합니다. pointButton은 자체 상태를 변경할 수도 있습니다. 스타일은 상태에 따라 바뀝니다. 여기서 포인트 버튼의 코드이다

I 스크롤
private var _medication :MBMedicationTaken? 
    var medication : MBMedicationTaken? { 
    set{ 
     self._medication = newValue 
     if self._medication!.taken == false{ 
     setNotTaken() 
     } else { 
     setTaken() 
     } 
    } 
    get{ 
     return self._medication 
    } 
    } 

, 포인트 버튼 변경 값이 연속적으로 배열 이전의 값으로 전송하기 때문이다. 스크롤 할 때 값이 변하지 않도록하려면 어떻게해야합니까? 트위터 즐겨 찾기 버튼과 비슷하다고 생각하십시오.

답변

0

가 지속적으로 당신이로드 할 때 데이터가 첫 번째로드에서 UITableView를 채우는 것을, 어떻게 배열

에서 이전 값을 통해 전송하기 때문이다. 더 이상 업데이트되지 않으므로 스크롤 할 때 UITableView는 초기 데이터 세트의 값을 사용합니다.

내가 알고있는 것처럼

의 pointsButton가있는 UIButton이며, 그이 가질 수있는 2 가지 상태 :

당신은 다음을 수행해야합니까? 또는? 그렇다면, 당신은해야한다 : 어떤 버튼을 누를 때

  1. 함수를 추가

    cell.pointsButton.addTarget(self, action: #selector(self.buttonAction(sender:)), 
           for: UIControlEvents.touchUpInside) 
    cell.pointsButton.tag = indexPath.row // add an unique identifier 
    
  2. 이 버튼을 누를 때 감지하는 버튼 액션을 추가

    func buttonAction(sender:UIButton!) { 
    
        let index = sender as! Int 
        // with this index, you know the correct index in your morningArray 
    
        // check if any value (i dont know how your object look like, for example ill use any Bool Value) 
    
        // change state 
    
        if(morningArray[index].selected == true) { 
         morningArray[index].selected = false 
        } else { 
         morningArray[index].selected = true 
        } 
    
    } 
    
  3. 이제 cellForRowAtIndexPath을 체크인 할 때 상태 값이 정확해야합니다.

    if(morningArray[indexPath.row].selected == true) { 
         // activate State Button 
        } else { 
         // disable State Button 
        } 
    
+0

이 작동하지만이 서로 다른 데이터 소스에있는 것은 정직 해킹의 일종 같은 느낌 것입니다. 어떤 의견? – spogebob92

+0

더 많은 코드를 입력하십시오.) – derdida

+0

@derdida가 맞습니다. 촬영 한 상태를 어딘가에 추적해야합니다. 셀은보기에 불과합니다. 자체 정보는 저장하지 않습니다. 기존 모델 개체를 확장하여이 정보를 저장하거나 보조 데이터 구조를 사용해야합니다. 그렇다면 어레이가 아닌 NSMutableIndexSet을 제안합니다. – Paulw11

관련 문제