2015-09-26 139 views
1

我有两个集合视图和一个显示名称,另一个显示相应人员的年龄。这些数据以“[[”Name“,”Age“],[”Name“:”Daniel“,”Age“:”20“],[”Name“:”Jake“ ,“Age”:“20”]]。这个数据来自于CSV文件,所以第一个元素是一个头。在collectionView cellForItemAtIndexPath里面,我正在检查集合视图并且提供数据库像row [rowPath.row] [ “名称”]和小区2 [indexPath.row] [ “时代” 然而,indexPath.row总是返回零,所以我想起来了头 -indexPath.row总是返回零

enter image description here

如何解决这个问题?这是我的代码 -

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 2 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 1 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if collectionView == self.nameCollectionView { 
     let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell 

     nameCell.data.text = self.data?[indexPath.row]["Name"] 
     println(indexPath.row) 

     return nameCell 
    } 
    else{ 
     let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell 

     ageCell.data.text = self.data?[indexPath.row]["Age"] 



     return ageCell 
    } 

} 
+1

如果您正在设置项目1的编号,那么您是如何得到indexPath.row多于0的? –

+0

尝试将numberOfitems返回到self.data.count –

回答

4

因为你的代码你只设置numberOfItemsInSection然后你总是得到第0个索引。使例如返回Array.count有动态值。

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.data.count // here you need to set dynamic count of array 
} 

UPDATE:

如果按照numberOfSectionsInCollectionView然后让你的代码像下面的cellForRow

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if collectionView == self.nameCollectionView { 
     let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell 

     nameCell.data.text = self.data?[indexPath.section]["Name"] 
     println(indexPath.section) 

     return nameCell 
    } 
    else{ 
     let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell 

     ageCell.data.text = self.data?[indexPath.section]["Age"] 



     return ageCell 
    } 

} 
+0

当我这样做时,我得到了多个不是我想要的列。 – user30646

+0

首先用你的逻辑清楚你想要什么。和'numberOfItemsInSection'返回特定段中的行数。代表indexPath.row。和'numberOfSectionsInCollectionView',它返回代表indexPath.section的节的数量。现在你需要考虑和使用,因为你需要 –

+0

这工作!谢谢! – user30646

0

正如其他人所指出的那样,你是,你总是告诉你收集意见在每个部分有2个部分和1个项目。因此,收集视图将只在每个部分中要求1个项目。因此,每个部分只有一个项目(索引0)。

你说“此数据存储内部字典的形式......”

它是一本字典或词典的阵列?字典是一个无序的集合,所以它不适合存储一组有序的项目以供给集合视图或表视图。一系列字典合适。从你显示的数据和你的代码,看起来你有一个字典数组。你应该编辑你的问题来说明清楚。

你的代码没有意义。您有2个不同的收集视图,并且每个视图都显示不同的数据。您告诉您的收藏视图您有两个部分,但忽略部分编号并为两部分创建相同的数据。那里有什么不对。

+0

对不起这是字典数组。我想要做的就是在一列中显示姓名,并在另一列中陈年。 – user30646

0

IndexPath是一个属性,它具有以下结构 Indexpath {Section, Row}。 所以如果你想你的数据在两个不同的部分,在这些单行然后indexpath.row为他们每个人是否会返回一个0作为,因为

对于部分指数0 - Indexpath[0,0]意义的部分索引0和行indexpath指数0

对于部分指数1 - Indexpath[1,0]意义的部分指标1和行标0

希望可以让您了解indexpath。