Stuck understanding how to create a table with multiple columns in iOS Swift

前端 未结 3 1321
借酒劲吻你
借酒劲吻你 2021-02-02 00:47

I\'ve spent the better half of the day so far researching and trying to understand how to make a table with multiple columns. Embarrassingly, I am still quite new to Swift and p

3条回答
  •  野性不改
    2021-02-02 01:12

    One approach is to use a custom cell in a tableviewcontroller. Your story board consists of a table in which the cell is a custom cell with UILabels for columns laid out next to each other (with properly defined constraints).

    Example code for the controllers looks like:

    import UIKit
    
    class TableViewController: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        // MARK: - Table view data source
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return 3
        }
    
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as TableViewCell
            cell.column1.text = "1" // fill in your value for column 1 (e.g. from an array)
            cell.column2.text = "2" // fill in your value for column 2
    
            return cell
        }
    
    }
    

    and:

    import UIKit
    
    class TableViewCell: UITableViewCell {
    
        @IBOutlet weak var column1: UILabel!
        @IBOutlet weak var column2: UILabel!
        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
        }
    
    }
    

提交回复
热议问题