[iOS] 테이블뷰 스와이프 액션swipe action

RudinP·2025년 5월 19일
0

Study

목록 보기
269/273

Swipe Action


  • 테이블뷰의 셀 스와이프 시 양쪽에 각각 1개 이상의 액세서리를 표시할 수 있게 해준다.

구현

leadingSwipeActionsConfigureationForRowAt

구현 순서

  1. 표시할 메뉴를 액션으로 만들기
  2. configuration 객체에 담기
  3. 리턴
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
	//표시할 메뉴를 액션으로 만들기
    let renameAction = UIContextualAction(style: .normal, title: "이름 바꾸기") { action, view, completion in
    	~실행할 코드~
        completion(true) //작업 성공시 true 전달
    }
    
    //configuration 객체에 담기
    let configuration = UISwipeActionsConfiguration(actions: [renameAction])
    
    return configuration
}

UIContextualAction 생성자

  • style: 기본적으로는 .normal, 삭제하는 행위인 경우 .destructive 사용
  • title: 액션 버튼의 이름
  • handler

  • action: action이 그대로 전달됨
  • UIView: 스와이프시 표시되는 버튼이 전달됨
  • @escaping (Bool) -> Void: 작업을 끝낸 뒤 호출되는 핸들러가 전달됨

액션의 뷰 수정하기

  • 회색 컬러, 이름바꾸기라는 텍스트를 아이콘으로 바꿀 수 있다.
//컬러 바꾸기
renameAction.backgroundColor = .systemBlue

//이미지 표시
renameAction.image = UIImage(systemName: "square.add.pencil")

profile
iOS 개발자가 되기 위한 스터디룸...

0개의 댓글