我正在使用Swift 3开发一个应用程序,但我遇到了问题,因为我想制作一个表:
好吧,我附上了“传奇类”的代码和“customCell”的代码:
class LeftSideViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var selectedIndex = -1
//var menuItems:[String] = ["RGB","PCB", "PCD Zonificado","PCD Arbol","Target Sectors", "Variability Map"]
@IBOutlet weak var tableSideLeft: UITableView!
@IBAction func opacityDelivery(_ sender: UISlider) {
print(sender.value)
}
override func viewDidLoad() {
super.viewDidLoad()
tableSideLeft.isEditing = true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return snapShotsLegend.legendEntries[0].deliverables.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! customCell
cell.firstViewLabel.text = snapShotsLegend.legendEntries[0].deliverables[indexPath.row].type
cell.secondViewLabel.text = "Option " + snapShotsLegend.legendEntries[0].deliverables[indexPath.row].type
cell.idDeliveryResponse.text = snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].id
cell.initialMaxDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].initial_max_value)
cell.initialMinDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].initial_min_value)
cell.maxRangeDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].max_range)
cell.minRangeDeliveryResponse.text = String(snapShotsLegend.legendEntries[0].deliverables[indexPath.row].options[0].min_range)
cell.backgroundColor = UIColor.init(red: 170/255, green: 170/255, blue: 170/255, alpha: 1)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(selectedIndex == indexPath.row){
return 300
}else{
return 60
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if(selectedIndex == indexPath.row){
selectedIndex = -1
}else {
selectedIndex = indexPath.row
}
self.tableSideLeft.beginUpdates()
self.tableSideLeft.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self.tableSideLeft.endUpdates()
}
//MARK: FUNCTIONS ALLOWS REORDERING OF CELLS
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = snapShotsLegend.legendEntries[0].deliverables[sourceIndexPath.row]
snapShotsLegend.legendEntries[0].deliverables.remove(at: sourceIndexPath.row)
snapShotsLegend.legendEntries[0].deliverables.insert(item, at: destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
}
自定义单元格是:
import UIKit
class customCell: UITableViewCell {
//MARK: OUTLETS VIEW 1
@IBOutlet weak var firstView: UIView!
@IBOutlet weak var firstViewLabel: UILabel!
@IBOutlet weak var swichtActiveLayer: UISwitch!
@IBOutlet weak var secondView: UIView!
@IBOutlet weak var secondViewLabel: UILabel!
@IBOutlet weak var secondHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var idDeliveryResponse: UILabel!
@IBOutlet weak var minRangeDeliveryResponse: UILabel!
@IBOutlet weak var maxRangeDeliveryResponse: UILabel!
@IBOutlet weak var initialMinDeliveryResponse: UILabel!
@IBOutlet weak var initialMaxDeliveryResponse: UILabel!
@IBAction func sliderOpacity(_ sender: UISlider) {
print(sender.value)
}
//MARK: OUTLETS VIEW 2
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
var showsDetails = false {
didSet {
secondHeightConstraint.priority = showsDetails ? 250 : 900
}
}
}
当我可以重新排序细胞时(uncoment line“tableSideLeft.isEditing = true “)我明白了:
当我评论“tableSideLeft.isEditing = true”这一行时你可以看到:
问题是,如果我离开“tableSideLeft.isEditing = true”这一行,我可以改变表的顺序。但是,我消失了扩大细胞的行动。但如果我留下那条评论,如果我可以扩展细胞,但不交换细胞。
我怎样才能让他们两个都做:)?
谢谢!
答案 0 :(得分:0)
我刚刚在另一个问题上发布了关于扩展/折叠表格视图单元格的答案。还有一个与github链接的工作示例项目:
Dynamic Sizing and Collapsable TableViewCells
至于更改顺序,通常是通过为tableview控制器输入“编辑模式”来完成的。编辑模式包括对插入,删除和重新排序控件的支持。该功能主要是内置功能,只需要一些代码来设置它。
要进入该模式,请进行以下呼叫。
self.tableView.setEditing(editing, animated: animated)
您可以在TableViewCell中配置编辑选项。在“界面”构建器中,选择原型单元格时,可以在检查器中设置以下内容:
最后一个是您要确保检查的内容。
Apple tableView.setEditing docs在描述中给出了不错的概述。
答案 1 :(得分:0)