如何将collectionviewcell的选定indexpath.row数作为slideshare

时间:2015-08-27 01:49:53

标签: ios iphone swift uicollectionview uicollectionviewcell

我希望将集合视图中所选单元格的数量设置为“10/53”

下面的幻灯片共享应用程序

我厌倦了许多方法,例如: “(print [indexpath.row])”或打印“cell [indexpath.row] .count”

enter image description here

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell


    // Configure the cell
    if indexPath.row == 0 {
     cell.txxt.text = "Biz memorabilia: 1st day in business, a cold & cloudy day in 1955, @McDonalds did $366.12 in revenue on 2 registers.Biz memorabilia: 1st day in business, a cold & cloudy day in 1955, @McDonalds did $366.12 in revenue on 2 registers."

    }
    if indexPath.row == 1 {
        cell.txxt.text = "SmartHalo: Turn any bike into a smart bike http://www.producthunt.com/tech/smarthalo  via @gozmike on @producthunt"

    }
    if indexPath.row == 2 {
        cell.txxt.text = "Traditionally, policymakers & nonprofits try to improve financial health by measuring & teaching financial literacy"

    }

    //slideNumber is uilabel in the collectionViewCell
    cell.slideNumber.text = "(print[indexpath.row])"

    return cell
}

1 个答案:

答案 0 :(得分:1)

使用visibleCells获取当前可见的 10 of 53

   func scrollViewDidScroll(scrollView: UIScrollView!) {

        for cell in collectionView.visibleCells() as [CollectionViewCell] {
            var indexPath : NSIndexPath = collectionView.indexPathForCell(cell as CollectionViewCell)!
            // do something
            // display 10 of 53 here        
        }
    }

单元格中的10/53

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell


    // Configure the cell
    if indexPath.row == 0 {
     cell.txxt.text = "Biz memorabilia: 1st day in business, a cold & cloudy day in 1955, @McDonalds did $366.12 in revenue on 2 registers.Biz memorabilia: 1st day in business, a cold & cloudy day in 1955, @McDonalds did $366.12 in revenue on 2 registers."

    }
    if indexPath.row == 1 {
        cell.txxt.text = "SmartHalo: Turn any bike into a smart bike http://www.producthunt.com/tech/smarthalo  via @gozmike on @producthunt"

    }
    if indexPath.row == 2 {
        cell.txxt.text = "Traditionally, policymakers & nonprofits try to improve financial health by measuring & teaching financial literacy"

    }

    //slideNumber is uilabel in the collectionViewCell
    cell.slideNumber.text =  NSString(format: "%d/%d", indexPath.row, totalCell) // totalCell is num of cells you have

    return cell
}