2016-10-18 3 views
1

선택한 행을 배열에 추가하고 해당 행을 선택 해제 할 때 배열에서 제거하는 함수를 만들려고합니다. 너희들이 나를 도와 줄 수있어?didSelectRow에 추가하고 신속하게 3.0에서 didDeselectRow를 제거하는 방법

행 선택 기능 :

var exercises = [exercise]() // Array that will be filled by selected rows 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell 
    let exercisesSelected = selectedCell?.exerciseNameLbl.text 
    exercisenames.insert(exercisesSelected!, at: 0) 
} 

이 내 선택 해제 위임 방법입니다 :

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell 

     if exercisenames.count < 1 { 
      print("empty array") 
     }else{ 
     exercisenames.remove(at: indexPath.row) 
     print(exercisenames) 
     } 
    } 

어떤 도움을 기대, 감사합니다!

케빈.

+0

다른 대리자 메서드'deselectRowAtIndexPath이 있습니다를'. 거기에서 배열에서 제거 할 수 있습니다. –

+0

당신이 묻고있는 것이 분명하지는 않습니다 ... 대개 데이터 배열 (셀로 나타낼 데이터)에서 객체를 추가/제거합니다. 따라서 배열을 기반으로 객체를 다른 객체로 옮기고 싶습니까? 이 경우 @ New16의 대답을 사용할 수 있지만 코드가 잘못되었습니다. – Zeb

+1

실제로는 선택 취소시 해당 오브젝트를 제거하려고합니다. 따라서 대리인이 처리해야합니다. –

답변

2

for in 루프를 사용하여 배열의 특정 개체를 제거 할 수 있습니다.

for (index,value) in exercisenames.enumerate() { 
    if value == selectedCell.exerciseNameLbl.text { 
     exercisenames.remove(at:index) 
    } 
} 
+0

이것은 내가 찾고 있었던 것이다! 위대한 코더, 감사합니다 –

+0

@KevinVugts 문제 없습니다 :) 나는 당신이 스위프트 가이드를주의 깊게 읽는 것이 좋습니다, 그것은 많은 도움이됩니다! –

0

u는 다음과 같이, 그것을 하나의 위임 FUNC를 사용할 수 있습니다이 U 도움이

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if exercisenames.count < 1 { 
     print("empty array") 
    } else { 

     //check if exercise exist in array 
     if exercisenames.contain(/*your exercise*/) == true { 
      exercisenames.remove(at: indexPath.row) 
      tableView.deselectRow(at: indexPath, animated: true) 
    } else { 
     //insert to array 
    } 
    } 

} 

메가바이트)

관련 문제