处于编辑模式时的 UITableViewCell 宽度问题
Posted
技术标签:
【中文标题】处于编辑模式时的 UITableViewCell 宽度问题【英文标题】:UITableViewCell Width Issue while in editing mode 【发布时间】:2017-01-12 10:46:20 【问题描述】:我正在使用 UITableview 开发拖动或重新排序列表视图。我没有为此使用情节提要并以编程方式执行整个功能。拖动功能正在工作,但 UITableViewCell 根据屏幕截图中的附件在其左侧和右侧放置空白空间。那么如何删除这个空格。仅当我们以编程方式执行此操作时才会出现此问题,如果我们为此使用情节提要,然后 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.
*/
【问题讨论】:
【参考方案1】:func loadViewControls()
tblTest.cellLayoutMarginsFollowReadableWidth = false
【讨论】:
以上是关于处于编辑模式时的 UITableViewCell 宽度问题的主要内容,如果未能解决你的问题,请参考以下文章
在iOS7中处于编辑模式时更改uitableviewcell“删除”部分的颜色
滑动手势取消编辑模式后 UITableViewCell 刷新不良
如何在自定义 UITableViewCell 中控制 UITextFields 的编辑