答案 0 :(得分:1)
方括号表示可选参数。只有在输入前面的可选参数时,才能输入一些可选参数。例如,如果您尚未输入equal to or greater then 0
和struct MenuTableCellContents {
var identifier: cellIdentifier
var cellImage: UIImage
var cellText: String
//var cellFont: UIFont
var cellTextColor: UIColor
var subCells: [MenuTableCellContents]?
/// Normal cells without subcells
init(identifier: cellIdentifier, cellImage: UIImage, cellText: String, /*cellFont: UIFont,*/ cellTextColor: UIColor) {
self.identifier = identifier
self.cellImage = cellImage
self.cellText = cellText
self.cellTextColor = cellTextColor
//self.cellFont = cellFont
}
/// Cells with subcells
init(identifier: cellIdentifier, cellImage: UIImage, cellText: String, /*cellFont: UIFont,*/ cellTextColor: UIColor, subCells: [MenuTableCellContents]) {
self.identifier = identifier
self.cellImage = cellImage
self.cellText = cellText
self.cellTextColor = cellTextColor
//self.cellFont = cellFont
self.subCells = subCells
}
enum cellIdentifier {
case standard
case noIcon
case subcell
}
}
class MenuTableCell: UITableViewCell {
@IBOutlet var cell_icon: UIImageView!
@IBOutlet var cell_label: UILabel!
@IBOutlet var cell_contentView: UIView!
@IBOutlet var cell_contentView_height: NSLayoutConstraint!
var cellImage: UIImage?
var cellText: String?
var cellTextColor: UIColor?
//var cellFont: UIFont?
var subCells: [MenuTableCellContents]?
private var contentViewHeight: CGFloat?
override func draw(_ rect: CGRect) {
drawSubCells()
cell_label.text = cell_label.text?.uppercased()
super.draw(rect)
}
/// draw the subcells when needed
func drawSubCells() {
// Check if there are subcells
if let cells = subCells {
let count = CGFloat(cells.count)
// Set the height for the subcells
contentViewHeight = 40 * count
let contentViewFrame = CGRect(x: self.frame.width, y: self.cell_contentView.frame.origin.y, width: self.frame.width, height: contentViewHeight!)
cell_contentView?.frame = contentViewFrame
var i = 0
for cell in cells {
// create a view for each cell
let cellView = UIView()
let cellFrame = CGRect(x: 0, y: CGFloat(i * 40), width: contentViewFrame.width, height: 40)
cellView.frame = cellFrame
// add a image view to each cell
let imageView = UIImageView(image: cell.cellImage)
let imageViewFrame = CGRect(x: 0, y: (cellFrame.height - cellFrame.height / 2) / 2, width: cellFrame.width * 0.2, height: cellFrame.height / 2)
imageView.frame = imageViewFrame
imageView.contentMode = .scaleAspectFit
// add a button to each cell
let cellLabel = UILabel()
let cellLabelFrame = CGRect(x: imageViewFrame.width, y: 0, width: cellFrame.width - imageViewFrame.width, height: 40)
cellLabel.frame = cellLabelFrame
//cellLabel.font = cell.cellFont
cellLabel.text = cell.cellText.uppercased()
cellLabel.textColor = cell.cellTextColor
cellLabel.minimumScaleFactor = 15
// add the imageview and button to the subcell view
cellView.addSubview(imageView)
cellView.addSubview(cellLabel)
// add the subcell to the cell
cell_contentView?.addSubview(cellView)
i += 1
}
setupConstraints(height: contentViewHeight!)
}
}
/// Set the height constraint for the cell content view in which the subcells reside
func setupConstraints(height: CGFloat) {
cell_contentView.frame.size.height = height
}
}
,则无法输入class MenuViewController: UIViewController, UITableViewDelegate {
@IBOutlet var menu_table: MenuTableView!
var cellContent: [MenuTableCellContents] = []
override func viewDidLoad() {
super.viewDidLoad()
setupCells()
let menuCell = UINib(nibName: "MenuTableCell", bundle: nil)
menu_table.register(menuCell, forCellReuseIdentifier: "menuCell")
menu_table.delegate = self
menu_table.dataSource = self
menu_table.rowHeight = UITableViewAutomaticDimension
menu_table.estimatedRowHeight = 50
self.revealViewController().frontViewShadowRadius = 0
self.revealViewController().frontViewShadowOffset = CGSize.zero
}
/// setup the required cells for the menu
func setupCells() {
let normal1 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal1", cellTextColor: UIColor.black)
let normal2 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal2", cellTextColor: UIColor.black)
let normal3 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal3", cellTextColor: UIColor.black)
let sub1 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub1", cellTextColor: UIColor.black)
let sub2 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub2", cellTextColor: UIColor.black)
let sub3 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub3", cellTextColor: UIColor.black)
let subs = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "subs", cellTextColor: UIColor.black, subCells: [sub1, sub2, sub3])
let normal4 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal4", cellTextColor: UIColor.black)
cellContent = [normal1, normal2, normal3, subs, normal4]
}
}
/// required methods for the uitableview
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let content = cellContent[indexPath.item]
let cell = menu_table.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! MenuTableCell
cell.cell_icon.image = content.cellImage
cell.cell_label.text = content.cellText
cell.cell_label.textColor = content.cellTextColor
//cell.cell_label.font = content.cellFont
if let subCells = content.subCells {
cell.subCells = subCells
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
参数。这解释了为什么在功能描述中你有几个括号在你可以关闭它们之前打开。