ios - 如何根据 UICollectionView 的部分以编程方式更新标签

标签 ios swift xcode uicollectionview

假设我有一个包含 3 个部分的水平 UICollectionView。我希望它根据用户滚动时所在的部分立即更新标签。我找到了一些通过使用 scrollViewDidEndDecelerating 来实现此目的的解决方案,但我希望它在从第 0 部分到第 1 部分、从第 1 部分到第 2 部分等时进行更新。

有什么想法吗?

最佳答案

let indexPath = tableView.indexPathForSelectedRow();
var sectionNumber = indexPath.section

并检查 0 是否显示更新第一个标签等,但我认为索引行的单元格中的最佳位置并获取节号,然后更新其标签

还有另一种解决方案

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

            //Create label and autoresize it
            let headerLabel = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 2000))
            headerLabel.font = UIFont(name: "Avenir-Light", size: 30)
            headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
            headerLabel.sizeToFit()

            //Adding Label to existing headerView
            let headerView = UIView()
            headerView.addSubview(headerLabel)

            return headerView
}

也试试这个

 func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "Futura", size: 11)
    header.textLabel?.textColor = UIColor.lightGrayColor()
}

关于ios - 如何根据 UICollectionView 的部分以编程方式更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447948/

相关文章:

ios - 展开协议(protocol)类型的可选弱属性时的 Swift EXC_BAD_ACCESS

SwiftUI 观察到的对象在发布值更改时不更新

ios - 将视频及其缩略图保存和检索到应用程序的目录

ios - UIViewController 中的 SKScene

ios - swift/Xcode6 : How to change when the UIButton functionality is called after clicking the button

ios - 使用 RestKit 在 iOS 中解析 JSON 失败

ios - 给定名称不显示为应用程序名称,安装应用程序时,项目名称显示为应用程序名称,在 iOS、Apple、AppStore

ios - 将多个布局约束数组附加到单个 Swift 数组中?

ios - 循环遍历数组内容的名称

iphone - 如何将 Youtube 视频添加到 UITableView?