UITableViewCell宽度在编辑模式下发出

时间:2017-01-12 10:46:20

标签: swift uitableview tableviewcell programmatically-created

我正在使用UITableview开发拖动或重新排序列表视图。我没有使用storyboard并以编程方式执行整个功能.Draging功能正在运行,但是UITableViewCell在屏幕截图中按照附加的方式在其左侧和右侧放置空白区域。所以如何删除这个空白区域。只有当我们使用storyboard时才会以编程方式执行此操作,然后uitableviewcell不会放置任何空格。

类ArrangeBinaryViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

// MARK: - Attributes -
var tblTest : UITableView!
var arrTest : [String] = []

// MARK: - Lifecycle -
init(){
    super.init(nibName: nil, bundle: nil)
    self.loadViewControls()
    self.setViewlayout()
}

required init?(coder aDecoder: NSCoder)
{
    super.init(coder:aDecoder)
    self.loadViewControls()
    self.setViewlayout()
}

override func viewDidLoad()
{
    super.viewDidLoad()

    tblTest.setEditing(true, animated: true)
    // Do any additional setup after loading the view.
}

deinit{

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Layout -
func loadViewControls()
{
    arrTest.append("he Table View Controller has an editing mode, when enabled the rows can be reordered by dragging the cells up/down. In this tutorial we will fill the Table View with some data and put the Table View Controller in editing mode. This tutorial is built in iOS 8.1 and Xcode 6.1.")

    arrTest .append("Open Xcode and create a new Single View Application. For product name, use IOS8SwiftReorderingRowsTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.")

    arrTest .append("Remove the View Controller from the Storyboard and drag a Navigation Controller to the empty canvas. When the initial View Controller is deleted there isn't a starting point defined. Select the Navigation Controller and go to the Attribute Inspector. In the View Controller Section  elect the Is Initial View Controller checkbox.")

    arrTest .append("Double-click on the Navigation Bar in The Table View Controller and set the title to Numbers.  Select the Table View Cell and go to the Attributes Inspector. In the Table View Cell section set the Identifier  to Cell.")

    tblTest = UITableView(frame: CGRect.zero, style: UITableViewStyle.grouped)
    tblTest.backgroundColor = UIColor.brown
    tblTest.contentMode = .scaleToFill
    tblTest.delegate = self
    tblTest.dataSource = self
    tblTest.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    tblTest.translatesAutoresizingMaskIntoConstraints = false
    self.view .addSubview(tblTest)
}

func setViewlayout() {

    tblTest.expandIn(SuperView: self.view)

    self.view .addConstraint(NSLayoutConstraint(item: tblTest, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0))
    self.view .addConstraint(NSLayoutConstraint(item: tblTest, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0))
    self.view .addConstraint(NSLayoutConstraint(item: tblTest, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))
    self.view .addConstraint(NSLayoutConstraint(item: tblTest, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))

    self.view.layoutIfNeeded()
}

// MARK: - Public Interface -


// MARK: - User Interaction -


// MARK: - Internal Helpers -


// MARK: - Delegate Method -
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell : UITableViewCell!
    cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    cell.textLabel?.text = arrTest[indexPath.row]
    cell.textLabel?.numberOfLines = 0
    cell.showsReorderControl = true

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50.0
}

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    print("indexPath",indexPath.row)
    return true
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

}

func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {

    if sourceIndexPath.section == proposedDestinationIndexPath.section{
        return proposedDestinationIndexPath
    }
    return sourceIndexPath
}

// MARK: - Server Request -




/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

} enter image description here

1 个答案:

答案 0 :(得分:3)

func loadViewControls()

    tblTest.cellLayoutMarginsFollowReadableWidth = false
相关问题