2017-11-15 17 views
0

나는 주제 질문 소리가 복잡하다는 것을 안다. 내가 겪고있는 문제를 단순화하기 위해 최선을 다할 것입니다.Swift를 사용하여 UITableViewCell을 축소 할 때 UIButton 이미지를 변경하는 방법은 무엇입니까?

UITableView에 UIButton이 있습니다. UITableViewCell은 UITableView에 대해 구성됩니다. 비록 UIViewController 내에서 UIButton 액션을 사용하고 있습니다. 나는 단순히 행 높이를 변경하고 우선 순위 제약 조건 조작을 통해 UITableViewCells의 축소/축소를 설정했습니다. 언 콜라 싱이 발생하면 추가 정보가 셀 아래에 나타납니다. 이 모든 것이 완벽하게 작동합니다.

UIButton 축소 기본 상태를 클릭하면 UIButton 이미지가 다른 UIButton 이미지로 바뀝니다. 그런 다음 셀을 클릭하여 uncollapse UIButton 이미지를 클릭하면 초기 이미지로 돌아갑니다. 이것은 또한 그 반대도 마찬가지입니다. 따라서 행의 높이를 변경할 때마다 UITableView가 셀을 다시 작성하여 이전에 변경 한 내용이 표시되지 않도록합니다.

class NotificationBtn: UIButton { 
var bellToggle = Int() 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if bellToggle == 1 { 
     self.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)    // Bounce 
     self.setImage(UIImage(named: "bell_off"), for: .normal) 
     UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { self.transform = CGAffineTransform.identity }, completion: nil) 
     curImg = self.image(for: .normal)! 
     bellToggle = 2 

     super.touchesBegan(touches, with: event) 

    } else { 
     self.transform = CGAffineTransform(rotationAngle: 25 * 3.14/180.0) // Rotate via 25 degree - Bell ringing animation 
     self.setImage(UIImage(named: "bell_on"), for: .normal) 

     UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { self.transform = CGAffineTransform.identity }, completion: nil) 

     curImg = self.image(for: .normal)! 
     bellToggle = 1 

     super.touchesBegan(touches, with: event) 
    } 
} 
} 

이 시간 내 주셔서 감사합니다

다음
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "FavCell", for: indexPath) as? FavCell else { return UITableViewCell() } 

if let data = UserDefaults.standard.data(forKey: selectedCur), 
    let storedCurrencies = NSKeyedUnarchiver.unarchiveObject(with: data) as? [SelectedCurrency] { 
    selectedCurrencies = storedCurrencies 
    // SORT BY RANK 
    selectedCurrencies.sort(by: { $0.rank! < $1.rank! }) 
     if selectedCurrencies.count > 0 { 
      // CATCH ANY NIL VALUES AND PREVENT APP CRASHES 
      let noVal = selectedCurrencies[indexPath.row].rank ?? 0 
      let nameVal = selectedCurrencies[indexPath.row].name ?? "N/A" 
      let symbolVal = selectedCurrencies[indexPath.row].symbol ?? "N/A" 

      cell.configureCell(no: "\(noVal)", name: nameVal, symbol: symbolVal) 
     } 
    } 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if collapseIndex == indexPath.row { 
     collapseIndex = -1 
    } else { 
     collapseIndex = indexPath.row 
    } 

    tableView.beginUpdates() 
    tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    tableView.endUpdates() 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if collapseIndex == indexPath.row { 
     return 280 // When collapsed cell view displayed - Number will be adjusted after 
    } else { 
     return 120 // Height of initial cell contents that fits 
    } 
} 

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

내가 UIButtons 이미지를 변경하는 방법입니다

다음은 관련 코드입니다.

+0

'cellForRow' 메소드를 표시 할 수 있습니까? – trungduc

+0

요청시 추가됨. – sc13

+0

이 메소드'configureCell'에서 버튼의 이미지를 업데이트 했습니까? – trungduc

답변

1

당신은 옳은 것 같았습니다. reloadRows 메서드를 호출하면 cellForRowAt이 호출되고 다시로드 할 셀을 다른 셀로 바꿉니다. 셀이 바뀌어 등장한 버튼이 다른 버튼이됩니다. 그래서 버튼에 대한 이미지가 잘못되었습니다.

cellForRowAt 방법으로 해결하려면 collapseIndex으로 버튼 이미지를 확인하고 업데이트하십시오. 나는 그것이 효과가있을 것이라고 생각한다. 예를 들어, 내부에 cellForRowAt, 셀 생성 후

if collapseIndex == indexPath.row { 
    // Updated button with collapse state image 
} else { 
    // Updated button with uncollapse state image 
}