如何使用 UITableViewCell 中的 UILabel 切换可见性? [英] How to toggle visibility with the UILabel in UITableViewCell?

查看:25
本文介绍了如何使用 UITableViewCell 中的 UILabel 切换可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ViewController 中有这段代码,名为 PlayViewController:

I have this chunk of code in my ViewController named PlayViewController:

words = [String]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "PlayTableViewCell"
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlayTableViewCell else {
        fatalError("The dequeued cell is not an instance of PlayTableViewCell.")
    }

    // Configure the cell...
    cell.wordLabel.text = words[indexPath.row]

    if (indexPath.row == 0) {
        cell.wordLabel.isHidden = false
    }

    return cell
}

这是我的名为 PlayTableViewCell 的 TableViewCell 代码:

And this is my code for TableViewCell named PlayTableViewCell:

import UIKit

class PlayTableViewCell: UITableViewCell {

    //MARK: Properties
    @IBOutlet weak var wordLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        wordLabel.lineBreakMode = .byWordWrapping;
        wordLabel.numberOfLines = 0;
        wordLabel.isHidden = true

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

正如预期的那样,只有我的第一个 wordLabel 出现在我的 tableView 中.我的目标是当用户在屏幕上的任意位置向右或向左滑动时显示第二个 wordLabel 并继续这种方式,直到用户显示最后一个 wordLabel.

As expected, only my first wordLabel appears in my tableView. My goal is to reveal the second wordLabel when the user swipes right or left anywhere on the screen and continue this way until the user reveal the last wordLabel.

我已经找到了如何设置滑动部分(只有向右滑动,添加左侧时表现怪异)但我无法弄清楚如何在检测到时切换 .isHidden 属性手势.

I've found how to set the swipe part (only the swipe right, behaves weirdly when the left is added) but I can't figure out how to toggle .isHidden property when I detect the gesture.

我不确定单元格配置是否在正确的路径上,但由于将 wordLabel 放置在 PlayTableViewCell 内,很难在函数 tableView.

I'm not sure to be on the right path with the cell configuration but because of the placing of the wordLabel inside PlayTableViewCell, it's hard to reach it outside the function tableView.

我既不能索引 cell 也不能索引 wordLabel,我不知道如何找到正确的 wordLabel 来切换它的可见性.

I can index neither cell nor wordLabel and I can't figure out how could I reach the right wordLabel to toggle its visibility.

推荐答案

Somewhere 存储显示标签总和的属性

Somewhere store property for sum of revealed labels

var numberOfRevealedLabels = 1

然后每次用户在某处滑动时,增加此值,然后重新加载表视图的数据(您可以在变量的 didSet 中重新加载它,或者在增加此值后,调用滑动)

then every time user swipes somewhere, increase this value and then reload data of your table view (you can reload it in didSet of your variable or after you increase this value in action which is called by swiping)

numberOfRevealedLabels += 1

现在,由于单元格是可重用的,根据 indexPath.row 是否小于或等于 numberOfRevealedLabels - 1

Now, since cells are reusable, set visibility depending on if indexPath.row is less then or equal to numberOfRevealedLabels - 1

cell.wordLabel.isHidden = !(indexPath.row <= numberOfRevealedLabels - 1)

... 这也涵盖了 indexPath.row 更大的情况

... this also covers the case that indexPath.row is greater

这篇关于如何使用 UITableViewCell 中的 UILabel 切换可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆