在Swift的iOS应用程序中显示一个表(在html / Excel电子表格中)
我一直在寻找有关如何做到这一点的解决方案,但我有点困惑.
我想在我的应用程序中显示数据表,而不是Apple的TableView,更像是在Excel或HTML中找到的表. 基本上就像这样一张桌子(因为我的数据是保密的,我把它放在谷歌图片上) 我有关于如何做到这一点的理论,但我不确定它是正确的还是确切地说如何做到这一点. 我的理论是我应该创建一个UICollectionView的子类,让我们称之为MyTablesView. MyTablesView将有一个名为data的变量.我的包含表格的页面/ VC将有一个MyTablesView实例,并在此变量中提供数据. MyTablesView负责样式设置和显示此数据. 我对Swift相当新,我不知道究竟是什么,所以我不确定如何解决这个问题.我对如何编写UICollectionView非常困惑.我试着阅读这个教程:http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1但它是一个UICollectionViewController而不是UICollectionView所以我不知道如何调整它. 所以现在这就是我所拥有的…… 所以我想我的问题是: 如果有什么不清楚,请告诉我. 解决方法
好吧,事实证明我没有在SO上彻底搜索,因为这是我一直在寻找的答案:
How to make a simple collection view with Swift 由于它是另一个Stack Overflow主题,我应该复制答案还是信任Stack Overflow来保持链接活着? 我链接的答案显示了一个数据网格,但我想要一个表,这意味着我需要稍微调整它,因为我想要行,所以下面是我的代码的概述.其中大部分与我所链接的答案相似.我所做的更改将数据显示在多维数组中,这意味着我还必须在我的类中添加numberOfSectionsInCollectionView方法并相应地调整其他方法. >在我的故事板中,我有一个VC,我拖动了一个UICollectionView.在它的第一个单元格中,我拖了一个UILabel. class MyView: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{ @IBOutlet weak var mytable: UICollectionView! var data = [[String]]() override func viewDidLoad() { data = [["first","1","2"],["second","3","4"]] } // --- UICollectionViewDataSource protocol --- //returns number of sections = subarrays in data func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{ return data.count } //returns number of items in a section= number of elements in a subarray in data func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int { return data[section].count } func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { // get a reference to our storyboard cell let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! MyTableCells // Use the outlet in our custom class to get a reference to the UILabel in the cell cell.myLabel.text = data[indexPath.section][indexPath.item] return cell } // --- UICollectionViewDelegate protocol --- func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) { // handle tap events print("You selected cell #(indexPath.item)!") } } import Foundation class MyTableCells: UICollectionViewCell { @IBOutlet weak var myLabel: UILabel! } 在故事板中,我将委托和数据源挂钩到VC(选择UICollectionView,单击圆圈并将其拖动到VC) 我现在只为我的一个VC做过,我实际上可能是UIViewController的子类,因为我猜这个实现可以有多个VC. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |