ํ ์ด๋ธ ๋ทฐ์ reorder ๋ฒํผ์ ๋ณ๊ฒฝํด์ผ ํ๋๋ฐ, API๊ฐ ์์ด ๊ณ ์ํ๋ ๊ฒฝํ์ ๊ณต์ ํ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
UITableView
์ tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
์์ cell ๋ด๋ถ์ ์๋ UITableViewCellReorderControl
๋ฅผ ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
TableView
extension SomeViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
self.moveRow(at: sourceIndexPath.row, to: destinationIndexPath.row) // dataSource ๋ณ๊ฒฝ
}
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let selectedCell = cell as? ReorderControlAppearanceReplaceable else {
return
}
selectedCell.replaceReorderImage()
}
}
Cell
internal protocol ReorderControlAppearanceReplaceable {
func replaceReorderImage()
}
internal class SomeCell: UITableViewCell, ReorderControlAppearanceReplaceable {
private let reorderControlView = UIImageView(image: UIImage(image: "์ด๋ฏธ์ง ์ด๋ฆ"))
internal func replaceReorderImage() {
guard let reorderControlView = self.subviews.filter({ String(describing: type(of: $0)) == "UITableViewCellReorderControl" }).first else {
return
}
_ = reorderControlView.subviews
.first(where: { $0 is UIImageView })
.map {
$0.removeFromSuperview()
}
self.reorderControlView.frame.size = CGSize(width: Constants.reorderViewWidth, height: Constants.reorderViewWidth)
reorderControlView.addSubview(self.reorderControlView)
self.reorderControlView <- Layouts()
.move(to: .right, margin: .zero, alignment: nil)
.move(toCenter: .vertical)
}
}
์ ๋ฆฌ
- ์ฝ๊ฐ hack ๊ฐ์ ๋๋์ด ์์ง๋ง, ํ์ฌ๋ก์๋ ์ด ๋ฐฉ๋ฒ๋ฐ์ ์๋ค๊ณ ํ๋ค.
Reference
- Change default icon for moving cells in UITableView
- UITableViewCellEditControlใจUITableViewCellReorderControlใฎ็ปๅใๅคๆดใใ(่คๆฐ้ธๆๅฏ่ฝๆใฎใฟ)
- https://velog.io/@ddosang/iOS-UITableView-%EC%88%9C%EC%84%9C-%EB%B3%80%EA%B2%BD-UITableViewReorderControl
- [iOS] UITableView Drag & Drop์ผ๋ก Row ์ด๋
- Reordering UITableView without reorder control