了解如何在iOS Swift中创建包含多个列的表 [英] Stuck understanding how to create a table with multiple columns in iOS Swift

查看:241
本文介绍了了解如何在iOS Swift中创建包含多个列的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我花了一半时间研究并试图了解如何制作包含多列的表格。令人尴尬的是,我对Swift和一般的编程还是很陌生,所以很多我读过和发现的东西都没有给我太多帮助。

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 programming in general so a lot of the stuff I've read and found aren't helping me too much.

我有基本上找到了我想要用这个绅士的blo创造的东西:
http://www.brightec.co.uk/blog/uicollectionview-using-horizo​​ntal-and-vertical-scrolling-sticky-rows-and-columns

I have basically found exactly what I want to create with this gentleman's blo: http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns

然而,即使和他的Github我仍然感到困惑。好像他根本没有使用Storyboard(对于我的项目,我一直在使用故事板)。我假设这是正确的吗?

However, even with his Github I'm still confused. It seems as if he did not use Storyboard at all (and for my project I've been using storyboard a lot). Am I correct in assuming this?

到目前为止我所拥有的是一个嵌入在导航控制器中的UICollectionView。从这里开始,我在CollectionView中创建了一个新的cocoa touch类文件。但是从这里开始我并不完全确定去哪里。

What I have so far is a UICollectionView embedded in a navigation controller. From here, I have created a new cocoa touch class file subclassed in the CollectionView. But from here is where I'm not entirely sure where to go.

如果我可以就这里的去向或如何正确设置提供一些方向,那将非常感激。

If I can have some direction as to where to go from here or how to properly set it up that would be GREATLY appreciated.

非常感谢提前!

推荐答案

一种方法是在tableviewcontroller中使用自定义单元格。您的故事板由一个表组成,其中单元格是一个自定义单元格,其中UILabel用于彼此相邻的列(具有正确定义的约束)。

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
    }

}

和:

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
    }

}

这篇关于了解如何在iOS Swift中创建包含多个列的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆