如何在Tableview处于编辑模式时编辑tableViewCell

时间:2017-03-14 19:16:16

标签: ios swift uitableview swift3 tableviewcell

我正在制作像whatsApp这样的应用程序请检查whatsApp调用选项卡。当我们点击时,点击编辑当tableview处于编辑模式时我应该如何向左移动单元格。请忽略Ancher函数,我创建了一个单独的函数来处理锚定。 ScreenShot 1 ScreenShot 2

import UIKit

class CallsTabController: UITableViewController {


let id = "reuseIdentifier"
var arr = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(cell.self, forCellReuseIdentifier: id)
    arr = ["Sachin","Papa","Mummy","Sachin","Papa","Mummy","Sachin","Papa","Mummy"]
    setupNav()
}


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arr.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell1 = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)as! cell

    cell1.data = arr[indexPath.row]

    return cell1
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

 return true

 }

 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

 if editingStyle == .delete {
    arr.remove(at: indexPath.row)
    print("Delete")
    tableView.deleteRows(at: [indexPath], with: .fade)
    tableView.reloadData()
 }
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){

    cell.separatorInset =  UIEdgeInsetsMake(0, 30, 0, 0)
    tableView.separatorInset =  UIEdgeInsetsMake(0, 30, 0, 0)


}



//






func add() {

}

func EditAction() {

    tableView.isEditing = !tableView.isEditing

    if tableView.isEditing == true {

        self.navigationItem.leftBarButtonItem?.title = "done"
        self.navigationItem.rightBarButtonItem =  UIBarButtonItem(title: "clear", style: .plain, target: self, action:#selector(self.EditAction))

    }
    else {

        self.navigationItem.leftBarButtonItem?.title = "Edit"
        let button1 = UIBarButtonItem(image: #imageLiteral(resourceName: "Calls"), style: .plain, target: self, action: #selector(self.add))
        self.navigationItem.rightBarButtonItem = button1
    }

}

func setupNav(){

    let button1 = UIBarButtonItem(image: #imageLiteral(resourceName: "Calls"), style: .plain, target: self, action: #selector(self.add))

    self.navigationItem.rightBarButtonItem = button1
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action:#selector(self.EditAction))

    self.navigationItem.titleView = {
        let search = UISegmentedControl(items: ["All","Missed"])
        search.apportionsSegmentWidthsByContent = false
        search.selectedSegmentIndex = 0
        return search
    }()
    self.tableView.addSubview(UISearchBar())


    }
}



class cell : UITableViewCell {


let frameview = { () -> UIView in 
    let view = UIView()
    return view
}()

var Name = { () -> UILabel in
    let lab = UILabel()
    lab.font = UIFont(name: "HelveticaNeue-Light", size: 16.0)
    lab.textColor = UIColor.black

    return lab
}()
var Description = { () -> UILabel in
    let lab = UILabel()
    lab.font = UIFont(name: "helveticaNeue-UltraLight", size: 12.0)
    lab.textColor = UIColor.black


    return lab
}()
var TimeLabel = { () -> UILabel in
    let lab = UILabel()
    lab.textAlignment = .right
    lab.font = UIFont(name: "helveticaNeue-light", size: 12.0)
    lab.textColor = UIColor.gray
    lab.text = "yesterday"
    return lab
}()
var infoBtn:UIButton = { () -> UIButton in
    let btn = UIButton()
    btn.setImage(#imageLiteral(resourceName: "info"), for: .normal)
    return btn
}()
var Calltype:UIImageView = { () -> UIImageView in
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.image =  #imageLiteral(resourceName: "Recivecall")
    return imageView
}()

var  data:String? {
    didSet {
        Name.text = self.data
        Description.text = "home"
    }
}
var frameLeftAncherValue:CGFloat?{
    didSet {
        Ancher()
    }
}
var frameRightAncherValue = 0



override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    Setup()
   Ancher()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func Setup() {

    addSubview(frameview)
    addSubview(Calltype)
    addSubview(Name)
    addSubview(Description)
    addSubview(infoBtn)
    addSubview(TimeLabel)



}

func Ancher() {
    Calltype.anchor(frameview.topAnchor, left: frameview.leftAnchor, bottom: frameview.bottomAnchor, right: nil, topConstant: 8, leftConstant: 8, bottomConstant: 8, rightConstant: 0, widthConstant: 20.0, heightConstant: 0)

    infoBtn.anchor(nil, left: nil, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 16.0, widthConstant: 20.0, heightConstant: 20.0)
    infoBtn.anchorCenterYToSuperview()

    Description.anchor(nil, left: Calltype.rightAnchor, bottom: frameview.bottomAnchor, right: infoBtn.leftAnchor, topConstant: 1, leftConstant: 4, bottomConstant: 4, rightConstant: 4, widthConstant: 0, heightConstant: 0)

    Name.anchor(frameview.topAnchor, left: Calltype.rightAnchor, bottom: nil, right: infoBtn.leftAnchor, topConstant: 2, leftConstant: 4, bottomConstant: 0, rightConstant: 4, widthConstant: 0, heightConstant: 0)

    TimeLabel.anchor(nil, left: nil, bottom: nil, right: infoBtn.leftAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 0, heightConstant: 0)
    TimeLabel.anchorCenterYToSuperview()

    frameview.anchor(topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: frameLeftAncherValue ??
        0.0, bottomConstant: 0, rightConstant: CGFloat(frameRightAncherValue), widthConstant: 0, heightConstant: 0)

    }

}

1 个答案:

答案 0 :(得分:0)

您的图片看起来好像已将子视图添加到单元格view属性而不是contentView属性。要在编辑期间重新调整内容的大小,您必须将新的UIView添加到contentView属性。

示例:

func setupViews() {

    self.contentView.addSubview(frameview)
    self.contentView.addSubview(Calltype)
    self.contentView.addSubview(Name)
    self.contentView.addSubview(Description)
    self.contentView.addSubview(infoBtn)
    self.contentView.addSubview(TimeLabel)
}