问题描述
我正在使用 Swift MVVM 编写一个非常简单的 tableview 应用程序。应用程序上有一个关注按钮,其文本会在点击时更改为取消关注。但问题是它的值在滚动后变回原始值。
下面是我的代码
型号
struct Person{
var name: String
var username : String
var currentFollowing : Bool
let image : UIImage?
}
主要ViewController
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,PersonFollowingTableViewCellDelegate {
func PersonFollowingTableViewCell(_ cell: PersonFollowingTableViewCell,didTapWith array: Person) {
return
}
var person = [Person]()
let Vm = PersonFollowingTableViewviewmodel()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.dataSource = self
tableView.frame = view.bounds
self.tableView.reloadData()
}
// MARK: - Registering Tableview
public let tableView : UITableView = {
let table = UITableView()
table.register(FollowList_MVVM.PersonFollowingTableViewCell.self,forCellReuseIdentifier: FollowList_MVVM.PersonFollowingTableViewCell.identifier )
return table
}()
// MARK: - Configuring the tableview
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return Vm.personFollowingTableViewviewmodel.count
}
var selectedindexArray:[Person] = []
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: FollowList_MVVM.PersonFollowingTableViewCell.identifier,for: indexPath) as? PersonFollowingTableViewCell else{
return UITableViewCell()
}
cell.configure(with: Vm.personFollowingTableViewviewmodel[indexPath.row])
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView,diddeselectRowAt indexPath: IndexPath) {
selectedindexArray.append(Vm.personFollowingTableViewviewmodel[indexPath.row])
}
}
视图模型
lass PersonFollowingTableViewviewmodel {
public var personFollowingTableViewviewmodel: [Person] =
[
Person(name: "Ben",username: "Ben \(Int.random(in: 111..<999) )",currentFollowing: false,image: UIImage(systemName: "person.fill")),Person(name: "Ann",username: "Ann\(Int.random(in: 111..<999) )",Person(name: "Tom",username: "Tom\(Int.random(in: 111..<999) )",Person(name: "Lee",username: "Lee\(Int.random(in: 111..<999) )",Person(name: "Craig",username: "Craig\(Int.random(in: 111..<999) )",image: UIImage(systemName: "person.fill"))
]
Tableview 单元格
protocol PersonFollowingTableViewCellDelegate : AnyObject {
func PersonFollowingTableViewCell( _ cell: PersonFollowingTableViewCell,didTapWith : Person)
}
let Vm1 = PersonFollowingTableViewviewmodel()
class PersonFollowingTableViewCell: UITableViewCell {
private var person : Person?
static let identifier = "PersonFollowingTableViewCell"
weak var delegate : PersonFollowingTableViewCellDelegate?
// MARK: - Customise the button and Label Color
public let userImageview: UIImageView = {
let imageView = UIImageView()
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
return imageView
}()
public let usernameLabel : UILabel = {
let label = UILabel()
label.textColor = .secondaryLabel
return label
}()
public var nameLabel : UILabel = {
let label = UILabel()
label.textColor = .black
return label
}()
public let button : UIButton = {
let button = UIButton()
return button
}()
// MARK: - Customise the button and Label Color
override init(style : UITableViewCell.CellStyle,reuseIdentifier : String?){
super.init(style: style,reuseIdentifier: reuseIdentifier)
contentView.addSubview(usernameLabel)
contentView.addSubview(nameLabel)
contentView.addSubview(userImageview)
contentView.addSubview(button)
contentView.clipsToBounds = true
button.addTarget(self,action: #selector(didTapButton),for: .touchUpInside)
}
// MARK: - Configure cell from viewmodel
required init?(coder: NSCoder) {
fatalError()
}
let defaultPerson = Person(name: "default",username: "default",currentFollowing: true,image: nil)
let Vc = ViewController()
@objc private func didTapButton(){
let currentFollowing = !(person?.currentFollowing ?? true)
person?.currentFollowing = currentFollowing
delegate?.PersonFollowingTableViewCell(self,didTapWith: person ?? defaultPerson)
configure(with: person ?? defaultPerson)
}
func configure(with person1 : Person){
self.person = person1
nameLabel.text = person1.name
usernameLabel.text = person1.username
userImageview.image = person1.image
if person1.currentFollowing{
Unfollow
}
else{
Follow
}
}
// MARK: - Customise the TableView hight,width
override func layoutSubviews() {
super.layoutSubviews()
let imageWidth = contentView.frame.size.height-10
userImageview.frame = CGRect(x: 5,y: 5,width: imageWidth,height: imageWidth)
nameLabel.frame = CGRect(
x: imageWidth+10,y: 0,width: contentView.frame.size.width - imageWidth,height: contentView.frame.size.height/2)
usernameLabel.frame = CGRect(
x: imageWidth+10,y: contentView.frame.size.height/2,height: contentView.frame.size.height/2)
button.frame = CGRect(
x: contentView.frame.size.width-120,y: 10,width: 110,height: contentView.frame.size.height-20)
}
var Unfollow : Void {
button.setTitle("Unfollow",for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.red.cgColor
button.layer.backgroundColor = UIColor.blue.cgColor
button.layer.cornerRadius = 8
}
var Follow : Void{
button.setTitle("Follow",for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.blue.cgColor
button.layer.backgroundColor = UIColor.red.cgColor
button.layer.cornerRadius = 8
button.layer.borderColor = UIColor.blue.cgColor
}
override func prepareForReuse() {
super.prepareForReuse()
nameLabel.text = person?.name
usernameLabel.text = person?.username
userImageview.image = person?.image
button.setTitle("Unfollow",for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.red.cgColor
button.layer.backgroundColor = UIColor.blue.cgColor
button.layer.cornerRadius = 8
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)