[ad_1]
In my app I’ve a display the place the person casts votes on a sure occasion, I alter the button picture based mostly on the vote of the person, If already voted the button picture stays the identical, else the picture adjustments. I need in order that when a vote is casted and the button picture adjustments, it stays in the identical state even after I depart the display and are available again till the ballot is up to date to a unique one, the button is inside a desk view cell, however that is proving to be fairly troublesome. right here is a few of my code from desk view aspect:
extension BuyTicketDetailsViewController: UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
if !(getPolls?.polls.isEmpty ?? false) {
return getPolls?.polls.depend ?? 1
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection part: Int) ->
UIView? {
let headerView = UIView.init(body: CGRect.init(x: 0, y: 0, width:
tableView.body.width, peak: 50))
let label = UILabel()
label.body = CGRect.init(x: 40, y: 20, width: headerView.body.width-10,
peak: headerView.body.height-10)
label.textual content = getPolls?.polls[section].ballot.description
label.font = .systemFont(ofSize: 16)
label.textColor = .black
label.font = UIFont(title:"Poppins-Medium", measurement: 20.0)
headerView.addSubview(label)
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection part: Int) ->
CGFloat {
return 50
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
if !(getPolls?.polls.isEmpty ?? false) {
return getPolls?.polls[section].choices.depend ?? 0
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"EventsPollTableViewCell", for: indexPath) as! EventsPollTableViewCell
cell.tag = indexPath.row
let sIndex = indexPath.part
if((getPolls?.polls.isEmpty) == nil){
tableView.isHidden = true
}else{
let depend = Double(getPolls?.polls[indexPath.section].choices[cell.tag].depend
?? 0)
let progress = Float(depend/100)
if getPolls?.polls[sIndex].choices[cell.tag].user_voted == true {
getIndex = indexPath.row
print(indexPath.row)
print(cell.tag)
}
if cell.tag == getIndex {
cell.eventsViewYesBtn.setImage(UIImage(named: "test"), for: .regular)
}
cell.delegate = self
cell.vc = self
cell.eventsViewYesBtn.tag = indexPath.row
cell.getPolls = self.getPolls
// cell.descriptionLbl.textual content =
getPolls?.polls[indexPath.section].choices[indexPath.row].description
// cell.votesCountLbl.textual content =
String((getPolls?.polls[indexPath.section].choices[indexPath.row].depend)!)
cell.descriptionLbl.textual content =
getPolls?.polls[indexPath.section].choices[indexPath.row].description
cell.votesCountLbl.textual content =
String((getPolls?.polls[indexPath.section].choices[indexPath.row].depend)!)
// cell.eventsViewYesBtn.addTarget(self,
motion:#selector(eventsYesBtnTapped(_:)), for: .touchUpInside)
cell.yesView.setProgress(progress, animated: true)
}
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath:
IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
right here is my api response mannequin:
struct PollsResponseModel: Decodable {
let standing: Bool
let message: String
let polls: [PollElement]
}
struct PollElement: Decodable {
let ballot: PollPoll
let choices: [Option]
}
struct Choice: Decodable {
let id,pool_id: Int
let description: String
let depend: Int
let user_voted: Bool
}
struct PollPoll: Decodable {
let id: Int
let description: String
let event_id: Int
}
my customized desk view cell code:
import UIKit
protocol EventsCellCallBack: AnyObject{
func userVotedSuccessfully()
}
class EventsPollTableViewCell: UITableViewCell, GetPollViewDelegate {
@IBOutlet weak var yesView: UIProgressView!
@IBOutlet weak var eventsViewYesBtn: UIButton!
@IBOutlet weak var descriptionLbl: UILabel!
@IBOutlet weak var votesCountLbl: UILabel!
var mainEvents: Particulars?
var delegate: EventsCellCallBack?
var getPolls: PollsResponseModel?
var vc = UIViewController()
// var getDetails: BuyTicketDetailsViewController?
lazy var viewModel:GetPollsViewModel = {
let viewModel = GetPollsViewModel()
viewModel.delegate = self
return viewModel
}()
override func awakeFromNib() {
tremendous.awakeFromNib()
self.yesView.layer.masksToBounds = true
self.yesView.progressTintColor = UIColor.blue.withAlphaComponent(0.3)
self.yesView.trackTintColor = .clear
self.yesView.layer.borderWidth = 1
self.yesView.layer.borderColor = UIColor.grey.withAlphaComponent(0.3).cgColor
self.yesView.layer.cornerRadius = 10
}
override func setSelected(_ chosen: Bool, animated: Bool) {
tremendous.setSelected(chosen, animated: animated)
}
@IBAction func eventsYesBtnTapped(_ sender: UIButton) {
guard let pollId = self.getPolls?.polls[0].ballot.id else {return}
guard let selectedOptionId = self.getPolls?.polls[0].choices[sender.tag].id else
{return}
viewModel.castVotef(CastVoteRequestModel(pool_id: pollId, option_id:
selectedOptionId))
}
func didFinishApiCallWithError(message: String) {
Toast.present(message: message, controller: self.vc)
}
func didupdatePollsSuccesfully(_ viewModel: GetPollsViewModel, information:
PollsResponseModel) {
self.stopLoading()
self.getPolls = information
}
func didCastVoteSuccessfully(_ viewModel: GetPollsViewModel, information:
CastVoteResponseModel) {
Toast.present(message: information.message, controller: self.vc)
self.delegate?.userVotedSuccessfully()
eventsViewYesBtn.isUserInteractionEnabled = false
// if getPolls!.polls[0].choices[0].user_voted == false &&
UserDefaults.commonplace.bool(forKey: "pulp") == true {
// eventsViewYesBtn.isUserInteractionEnabled = true
// eventsViewYesBtn.setImage(UIImage(named: "test"), for: [])
// eventsViewYesBtn.isUserInteractionEnabled = false
// UserDefaults.commonplace.set(false, forKey: "pulp")
//
// }
}
}
[ad_2]
