代码之家  ›  专栏  ›  技术社区  ›  Federica Benacquista

Swift:带有单元格的表格(相同的属性不同的内容;可点击)

  •  0
  • Federica Benacquista  · 技术社区  · 7 年前

    我需要创建一个包含50个矩形单元格的表,并且:
    所有单元格必须具有相同的属性,但内容不同;
    每个单元格必须是可点击的;
    单元格内必须有不同的项目(图像、标签等)。 我最近开始编程,我16岁,所以请不要咬我的头,如果我不清楚或什么。。。

    顺便说一句,我试图创建一个带有自定义单元格的表视图,但我不确定它是否正常工作,另外我处于横向模式,所以可能我需要以编程方式创建它们。。。你能不能告诉我,如果我做的是正确的使用自定义单元格,也许给我一些建议或寄给我这些事情我需要做一些教程?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Brian Tompsett - 汤莱恩 andrewwong97    7 年前

    别担心。继续你的事业。这里有一些例子给你。

    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
        }
    }
    

    希望对你有帮助。干杯。