快速連結

2023年6月14日

[Swift]UITableView想要可以移動cell重新排序、但是不顯示尾端的漢堡排圖案

直接上code!


class DataViewController:UIViewController, UITableViewDelegate, UITableViewDataSource, UITableViewDragDelegate, UITableViewDropDelegate{

    @IBOutlet weak var tableView: UITableView!
    var list: [String].= ["AAA","BBB"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dragDelegate = self
        tableView.dropDelegate = self
        tableView.dataSource = self
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "editAddressInputCell") as! EditAddressInputCells
        cell.text = list[indexPath.row]
        cell.canDrag = list > 1
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
        
    func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let itemProvider = NSItemProvider()
        return [UIDragItem(itemProvider: itemProvider)]
    }
    
    func tableView(_ tableView: UITableView, dragSessionWillBegin session: UIDragSession) {
        //移動開始時要做的動作(例如把編輯按鈕隱藏起來)
    }
    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        changeList(sourceIndex: sourceIndexPath.row, destinationIndex: destinationIndexPath.row)
    }
    
    func tableView(_ tableView: UITableView, dropSessionDidEnd session: UIDropSession) {
        //移動結束時(無論是移動到一半取消還是真的移動結束)要做的動作(例如把隱藏的編輯按鈕給打開)
    }
    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
    }

    func changeList(sourceIndex: Int, destinationIndex: Int){
        if sourceIndex != destinationIndex{
            let item = list.remove(at: sourceIndex)
            list.insert(item, at: destinationIndex)
        }
        
    }
}



class DataCell:UITableViewCell{
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var reorderIcon: UIImageView!
    @IBOutlet weak var label: UILabel!
    
    var canDrag: Bool = false{
        didSet{
            if canDrag{
                reorderIcon.isHidden = false
                self.showsReorderControl = true
            }else{
                reorderIcon.isHidden = true
                self.showsReorderControl = false
            }
        }
    }
    
    var text : String = ""{
        didSet{
            self.label.attributedText = NSAttributedString(string: text, attributes: self.label.defaultTextAttributes)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        selectionStyle = .none
        bgView.layer.cornerRadius = 8
        label.backgroundColor = UIColor.darkGray
        label.textColor = UIColor.white
    }
}

沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT