关闭并重新打开应用程序时,关注并取消关注按钮问题

时间:2018-06-20 14:00:44

标签: ios xcode swift3 instagram acts-as-follower

Tab Bar Controller中,有四个选项卡,分别是主页,发现,通知和用户个人资料。 “发现”选项卡控制器列出Firebase中的所有用户。列出用户的用户名和关注按钮。如果当前用户点击关注,则标题将设置为关注。

protocol PeopleTableViewCellDelegate {
func goToProfileUserVC(userId: String)
}



@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var followButton: UIButton!

var delegate: PeopleTableViewCellDelegate?
var user: User? {
    didSet {
        updateView()
    }
}

func updateView() {
    nameLabel.text = user?.username
    if let photoUrlString = user?.profileImageUrl {
        let photoUrl = URL(string: photoUrlString)
        profileImage.sd_setImage(with: photoUrl, placeholderImage: UIImage(named: "placeholderImg"))

    }


    if user!.isFollowing! {
        configureUnFollowButton()
    } else {
        configureFollowButton()
    }

}

func configureFollowButton() {
    followButton.layer.borderWidth = 1
    followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
    followButton.layer.cornerRadius = 5
    followButton.clipsToBounds = true

    followButton.setTitleColor(UIColor.white, for: UIControlState.normal)
    followButton.backgroundColor = UIColor(red: 69/255, green: 142/255, blue: 255/255, alpha: 1)
    followButton.setTitle("Follow", for: UIControlState.normal)
    followButton.addTarget(self, action: #selector(self.followAction), for: UIControlEvents.touchUpInside)
}

func configureUnFollowButton() {
    followButton.layer.borderWidth = 1
    followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
    followButton.layer.cornerRadius = 5
    followButton.clipsToBounds = true

    followButton.setTitleColor(UIColor.black, for: UIControlState.normal)
    followButton.backgroundColor = UIColor.clear
    followButton.setTitle("Following", for: UIControlState.normal)
    followButton.addTarget(self, action: #selector(self.unFollowAction), for: UIControlEvents.touchUpInside)
}

func followAction() {
    if user!.isFollowing! == false {
        Api.Follow.followAction(withUser: user!.id!)
        configureUnFollowButton()
        user!.isFollowing! = true
    }
}

func unFollowAction() {
    if user!.isFollowing! == true {
        Api.Follow.unFollowAction(withUser: user!.id!)
        configureFollowButton()
        user!.isFollowing! = false
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.nameLabel_TouchUpInside))
    nameLabel.addGestureRecognizer(tapGesture)
    nameLabel.isUserInteractionEnabled = true
}

func nameLabel_TouchUpInside() {
    if let id = user?.id {
        delegate?.goToProfileUserVC(userId: id)
    }
}

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


}

}

此处是重复用户的代码:

     func loadUsers() {

  //        self.users = []

    API.User.observeUser { (user) in

        self.isFollowing(userId: user.id!, completed: { (value) in
            user.isFollowing = value

            self.users.append(user)
            self.tableView.reloadData()

        })
    }

}

我的问题:“发现”选项卡上显示的列表重复,并且当用户点击“跟随”按钮时,其标题设置为“跟随”。但是,如果用户关闭并重新打开该应用程序,则尽管数据已正确插入到Firebase中,但返回到“发现”选项卡后,先前遵循的用户按钮仍将保持跟随状态。

请多多帮助...

1 个答案:

答案 0 :(得分:1)

我将其写为答案,因为(可能)没有足够的空间来写它作为评论。 首先,您的设置看起来有些混乱,似乎有些细节是在本地处理的,而有些细节是在Firebase上处理的,您的代码中没有任何东西触发loadUsers()

我建议您进行以下设置:

  1. 创建一个名为followingUsers的类,为它们提供必要的属性,例如,然后设置初始化器。然后在您的var followingUsersArray = [followingUsers]()中添加ViewController
  2. obeserve Firebase函数放入您的viewDidLoad()中, 所有数据并将其放入您的followingUsersArray中,然后重新加载tableView
  3. 创建一个UITableViewCell类,并为其提供所需的属性,然后设置一种方法来配置单元格。从cellAtRow调用该方法,然后从followingUsersArray传递值。
相关问题