2017-05-02 3 views
1

각 섹션에 다양한 섹션과 동적 행 번호가있는 테이블 뷰가 있습니다. 첫 번째 섹션에서는 셀을 모두 선택합니다. 모든 셀을 수동으로 선택하는 빠르고 쉬운 방법이 있습니까? didSelect의 코드가 트리거되고 다음 번에 사용자가 셀을 눌러 didDeselect을 트리거합니까? 지금 내가 생각할 수있는 유일한 방법은 모든 섹션을 통해 루프, 그리고 0 < indexPath.row < section's data array count과를 통해 각 섹션 루프에서프로그래밍 방식으로 테이블 뷰에있는 모든 셀을 선택하여 다음에 호출 할 때 didDeselect를 호출합니다.

self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none) 
self.tableView(self.tableView, didSelectRowAt: indexPath) 

를 호출하지만 너무 많이 반복하지 않고 그것을 할 수있는 방법은 무엇입니까?

+1

데이터 소스에서 각 셀의 선택된 상태를 추적하는 것이 좋지 않습니까? – HMHero

+0

직접'didSelectRowAt'를 호출해서는 안됩니다. 그것은 사용자가 무엇을했는지 알려주는 것입니다. 행이 선택되었다는 것을 이미 알고 있기 때문에 명시 적으로'selectRow'를 호출 할 때 그것은 무의미합니다. – rmaddy

답변

0

NSTableView의 메소드는 selectAll입니다.

+0

이것은 iOS 및'UITableView'에 관한 것입니다. – rmaddy

0

아니오; 반복하지 않고서는 그것을 할 수있는 방법이 없습니다. UITableView을 확장하여보다 쉽게 ​​할 수 있습니다. 이것은을 수행해야합니다

extension UITableView { 

    func selectAll(animated: Bool = false) { 
     let totalSections = self.numberOfSections 
     for section in 0 ..< totalSections { 
      let totalRows = self.numberOfRows(inSection: section) 
      for row in 0 ..< totalRows { 
       let indexPath = IndexPath(row: row, section: section) 
       // call the delegate's willSelect, select the row, then call didSelect 
       self.delegate?.tableView?(self, willSelectRowAt: indexPath) 
       self.selectRow(at: indexPath, animated: animated, scrollPosition: .none) 
       self.delegate?.tableView?(self, didSelectRowAt: indexPath) 
      } 
     } 
    } 

} 

그냥 루트 수준에서 신속한 파일의 해당을 추가 한 다음 어떤 UITableViewtableView.selectAll()를 호출 할 수 있습니다.

관련 문제