【问题标题】:Maintaining button selected state inside cell保持单元格内的按钮选择状态
【发布时间】:2017-05-23 18:49:05
【问题描述】:

假设我在一个集合视图中有 3 个单元格。单元格内部是一个关注按钮,其中isSelected = true 状态的标题为followingisSelected = false 状态的文本为follow

第一个和第三个按钮的isSelected 状态为false,第二个按钮的isSelected 状态为true。这让它变得虚假、真实、虚假。

问题是,我想在重新加载集合视图时保持这些状态。每当我在 collectionView 中调用 pull 来刷新时,它都会读取 json 并将数据加载到 collectionView 中。由于单元格的重用方式,它最终会重新加载前一个单元格。

所以最初,isSelected 状态的顺序是 false、true、false。然后,由于单元格的重用方式,这将变为 false、false true。

然后,一旦我再次请求检查按钮的状态,它就会变回 false、true、false。但是,我想保持状态而不是检查状态。否则,它看起来有问题。一瞬间,按钮的isSelected 状态不正确。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserCell.reuseIdentifier, for: indexPath) as! UserCell

    cell.user = user?[indexPath.row]
//            cell.followButton.isSelected = false

    return cell

}

.

class UserCell: UICollectionViewCell {

var user: User? {
    didSet {
        followButton.user = user
    }

    lazy var followButton: FollowButton = {
        let button = FollowButton()
        return button
    }()
}

.

class FollowButton: UIButton {

var user: User? {
    didSet {
        checkIfUserIsFollowed()
    }
}

override var isSelected: Bool {
    didSet {
        self.layer.backgroundColor = isSelected ? UIColor.rgb(50, green: 205, blue: 50).cgColor
                                                : UIColor.white.cgColor
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    layer.cornerRadius = 4
    layer.backgroundColor = UIColor.white.cgColor
    translatesAutoresizingMaskIntoConstraints = false

    setTitle("Follow", for: .normal)
    setTitle("Following", for: .selected)

    contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)
    titleLabel?.font = UIFont(name: "HelveticaNeue", size: 13.0)
    contentHorizontalAlignment = .right

    setTitleColor(UIColor.rgb(50, green: 205, blue: 50), for: .normal)
    setTitleColor(UIColor.white, for: .selected)

    layer.borderWidth = 0.5
    layer.borderColor = UIColor.rgb(50, green: 205, blue: 50).cgColor
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


func checkIfUserIsFollowed() {
    // This is a request that checks where the logged in user is following the user inside the cell.  It either returns true or false and sets the state accordingly
}

}

【问题讨论】:

  • 你能提供你的func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell方法的实现吗?
  • @TarasChernyshenko 完成

标签: swift uitableview uicollectionview uicollectionviewcell tableviewcell


【解决方案1】:

为了有效地实现这一点,您需要为以下列表保存用户数据。然后将这些与在 tableview 中填充数据时从 api 获取的数据进行比较。或者您必须实时更新服务器上的数据并仅使用服务器数据更新表。

【讨论】:

  • 所以我有一个用户当前遵循的用户 ID 数组,该数组是在用户从服务器获取用户之前获取的。然后,我将 userIds 与获取的用户的 id 进行比较?
【解决方案2】:

不一定需要服务器端。你可以维护一个 Bool 数组。数组长度 = 项目数。在选择具有相反值的按钮更新数组时。

例如:最初你的数组看起来像 [false, false, false]

在第一个单元格中选择按钮。使用 [false, true, false] 更新数组并同时检查 cellForItemAt 如果数组索引处的值为 true 则显示跟随,否则显示跟随。

【讨论】:

  • 所以我要管理两个不同的阵列? user 的数组和 isFollowed 的数组?
  • 您的用户数组是字典数组?
  • 不,用户数组只是用户数组
  • 因此,如果它在您的用户对象中,您可以为自己保留一个键值。 User["isFollowing"] = false 最初然后在 didSelect 上更新它并在 cellForItemAt 中检查这个值对。如果为 true 则显示如下 else 。
  • 你能发布一个示例数据吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多