我需要创建一个包含50个矩形单元格的表,并且:
所有单元格必须具有相同的属性但内容不同;
每个单元格必须可点击;
单元格中必须包含不同的项目(图像,标签等)。
我是最近开始编程的,当时我16岁,所以如果我不清楚或其他原因,请不要犹豫。
通过我尝试使用自定义单元格创建表视图的方式,但是我不确定它是否正常运行,而且我处于横向模式,所以也许我需要以编程方式创建它们...您能告诉我是否我正在使用自定义单元进行正确的操作,也许会给我一些建议,或者给我一些有关我需要做的事情的教程?
答案 0 :(得分:0)
不用担心。取得成就。这是为您提供的一些示例。
您的ViewController
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableReuseIdentifier = "TableCell"
let tableView: UITableView! = {
let table = UITableView(frame: .zero, style: UITableViewStyle.grouped)
table.backgroundColor = .clear
table.translatesAutoresizingMaskIntoConstraints = false
table.isUserInteractionEnabled = true
table.isMultipleTouchEnabled = true
return table
}()
var items: [U]? = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
//registering your customCell into tableView
self.tableView!.register(MyCustomCell.self, forCellReuseIdentifier: tableReuseIdentifier)
}
public func setupViews() {
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items?.count ?? 0
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: MyCustomCell = tableView.dequeueReusableCell(withIdentifier: tableReuseIdentifier, for: indexPath) as! MyCustomCell
//here you can manipulate some data to insert on cell
return cell
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("When cell has been selected")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return true
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
}
您的自定义单元格:
class MyCustomCell: UITableViewCell {
// attribute example
let lblCompanyName: UILabel = {
let label = UILabel()
label.textColor = .lightGray
label.font = UIFont.systemFont(ofSize: 17, weight: .bold)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.6
label.numberOfLines = 2
label.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: UILayoutConstraintAxis.vertical)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
// attribute example
let lblFrequencies: UILabel = {
let label = UILabel()
label.textColor = .black
label.font = UIFont.systemFont(ofSize: 13, weight: .bold)
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.setContentHuggingPriority(UILayoutPriority.defaultLow, for: UILayoutConstraintAxis.vertical)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
// attribute like imageView example
let imageLogo: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFit
image.setContentCompressionResistancePriority(UILayoutPriority.defaultHigh, for: UILayoutConstraintAxis.vertical)
image.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: UILayoutConstraintAxis.horizontal)
return image
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
[imageLogo, lblCompanyName, lblFrequencies].forEach { (view) in contentView.addSubview(view) }
let top: CGFloat = 20, bottom: CGFloat = -20, leading: CGFloat = 20, trailing: CGFloat = -20
self.imageLogo.topAnchor.constraint(equalTo: contentView.topAnchor, constant: top).isActive = true
self.imageLogo.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: bottom).isActive = true
self.imageLogo.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: leading).isActive = true
self.imageLogo.widthAnchor.constraint(equalTo: imageLogo.heightAnchor).isActive = true
self.lblCompanyName.topAnchor.constraint(equalTo: contentView.topAnchor, constant: top).isActive = true
self.lblCompanyName.leadingAnchor.constraint(equalTo: self.imageLogo.trailingAnchor, constant: 20).isActive = true
self.lblCompanyName.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: trailing).isActive = true
self.lblFrequencies.topAnchor.constraint(equalTo: self.lblCompanyName.bottomAnchor, constant: 5).isActive = true
self.lblFrequencies.leadingAnchor.constraint(equalTo: self.imageLogo.trailingAnchor, constant: 20).isActive = true
self.lblFrequencies.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: trailing).isActive = true
}
}
希望它对您有所帮助。干杯。