IOS Swift - 如何在具有不同数据的集合视图中使用2个表视图

时间:2015-07-30 07:36:02

标签: ios swift

我正在开发一个应用程序,我有一个带有2个不同tableviews的集合视图。但我需要知道的是我如何能够识别当时哪个tableview被数据填充。

4 个答案:

答案 0 :(得分:2)

1)为2个表格视图使用不同的委托和数据源

2)为您的2张桌子创建出口并进行比较。委托和数据源方法传递对表的引用

实施例

@IBOutlet weak var table1: UITableView!
@IBOutlet weak var table2: UITableView!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.table1 {
        // something
    }
    if tableView == self.table2 {
        // something else
    }
}

答案 1 :(得分:1)

每个UIView对象都有一个tag属性,即used to identify a UIView at runtime

您可以这样设置:

myTableView1.tag = Int.min
myTableView2.tag = Int.max

以及DelegateDataSource方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
  let rowCount: Int
  if tableView.tag == Int.Min
  {
    rowCount = self.dataForTableView1.count
  } else 
  {
    rowCount = self.dataForTableView2.count
  }
  return rowCount
}

使用tag值来识别正确的TableView以及它。

答案 2 :(得分:0)

您需要为它们提供不同的标签,然后围绕使用这些标签构建您的方法。

答案 3 :(得分:0)

什么是CollectionView?

1.It's similar to a UITableView
2.Layout grid instead of columns.
3.Defined by the UICollectionViewFlowLayout.
4.Nees a custom UICollectionViewCell
5.Requires a data source and delegate

它可以像TableViews一样有多个部分。每个部分中的项目等同于TableView中的单元格。

UICollectionViewCells有一堆背景属性。拖入单元格的第一件事将发生在单元格的内容视图之上。它'一个像空tabelViewCell的属性。它中没有任何内容,它只是一个视图,因此您在该视图顶部拖动的任何内容都将可见。例如ImageView,LabelView等。

如果你打开cell.contentView背景颜色会遮挡它下面的任何东西。在cell.contentView下面是一个叫做cell.backgroundView / cell.selectedBackgroundView的东西。这些观点最初都是零,但我们可以用我们自己的观点填充它们。

只需将CollectionViewController拖到故事板并将其标记为初始控制器即可。看一下Document Outline面板(点击Xcode窗口底部的方块),你会看到CollectionViewController带有一个collectionView,而CollectionView中有一个collectionViewCell和一个控制单元格所在位置的UICollectionViewFlowLayout。

这是您可以享受所有乐趣的地方。您可以在其中放置任何类型的视图。祝你好运。

enter image description here