ios - 如何获取 collectionviewcell 的选定 indexpath.row 的数量作为 slideshare

标签 ios iphone swift uicollectionview uicollectionviewcell

我想在 “10/53”

下获取 Collection View 中选定单元格的数量作为幻灯片共享应用程序

我厌倦了很多方法都错了,例如: “(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
}

最佳答案

使用 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
}

关于ios - 如何获取 collectionviewcell 的选定 indexpath.row 的数量作为 slideshare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239414/

相关文章:

ios - 呈现一个导航栏和一个非 Root View 选项卡 Controller

iphone - RemoveAllObjects 等效于 NSArray

iphone - 如何在Xcode 4的Method Navigator中查看注释?

iphone - 如何从 NSMutableDictionary 中删除字典数组?

iOS-Charts StackedBar值显示

部分屏幕区域中类似 iOS UINavigationController 的行为 (2016)

ios - iOS 10 中的后台执行

swift - 如何添加一个漂浮在 UITableView 上的按钮

ios - 使用@IBDesignable 更改标签字体

ios - 有没有办法操纵 NSUserDefaults 存储的信息