티스토리 뷰

728x90
반응형

UITalbeView editingStyle allowsMultipleSelectionDuringEditing

allowsMultipleSelectionDuringEditing

  • editing mode 에서 체크마크 속서이 없어서 구현에 어려움을 느꼈다. 하지만 allowsMultipleSelectionDuringEditing 속성을 통해서 해결했다.

  • 이렇게 선택된 row 들의 정보는 아래의 속성을 통해서 리턴 가능하다.

  • tableView.indexPathForSelectedRow 현재 선택되어있는 cell의 index를 return

  • tableView.indexPathsForSelectedRows tableView가 선택된 cell들의 index를 배열로 return합니다.(multiple selection이 가능할 때 사용)

    → 출력해보면 [[0,0],[0,1]] 처럼 section 과 row 를 보여준다.

그래서 나는 cell 이 선택될 때 출력해보도록했다.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
        print(tableView.indexPathsForSelectedRows!)
    }
  1. 우선 테이블뷰에 설정을 해주어야합니다. 스토리보드로 진행해도 되고 코드로 작성해도 됩니다.
  • storyboard 진행
  • programmatically 진행
tableView.allowsMultipleSelectionDuringEditing = true
  1. cell 의 selectionStyle 을 설정

tableView(tableView:cellForRowAt indexPath:) 메서드를 통해서 selectionStyle 을 설정해주면 된다.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard  let cell = tableView.dequeueReusableCell(withIdentifier: ListCell.identifier) as? ListCell else {
            return UITableViewCell()
        }
//기본값이 .none 이 되어있기때문에 설정.
//옵션에 .blue .gray .default 가 있는데 다 같은 효과를 보여주고 있다.
        cell.selectionStyle = .blue

        return cell
    }
  • 위의 코드를 입력하지 않으면 이런식으로 눌러도 체크가 되지 않는다. 하지만, tableView(tableView:didSelectRowAt:) 메서드로 로그를 찍어보니까 처음 선택했을 때 찍히고 두번째 선택하면 찍히지 않는다. 선택이 해제가 된다는 의미이다.
  • 즉, 선택은 되는데 보이지만 않는 것이다. selectionStyle = .none 인 상태이다.

해결


출처

출처ㅣstackoverflow - Cells not getting selected in UITableView with allowsMultipleSelectionDuringEditing set in edit mode

728x90
반응형
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
링크
Total
Today
Yesterday