Change button state if a row in tableView is selected - ios

I have a tableView when a row is selected I would like to show a button on the bottom (outside of the tableView) to be shown (isEnabled = true) when I have no rows selected I want it to be set to disabled (isEnabled = false. How can I handle this the best?
Currently I have this logic but when I try to select multiple rows this gives me issues. Because the first select will enable it and the second will disable it.
Here is the code:
I store a true or false in a var on the top called: var someRowSelected = false
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
selectedCell?.contentView.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
if(!someRowSelected){
self.someRowSelected = true
nextBtn.isEnabled = true
nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
}else{
self.someRowSelected = false
nextBtn.isEnabled = false
nextBtn.backgroundColor = UIColor.darkGray
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
selectedCell?.textLabel?.textColor = UIColor.darkGray
if(!someRowSelected){
self.someRowSelected = true
nextBtn.isEnabled = true
nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
}else{
self.someRowSelected = false
nextBtn.isEnabled = false
nextBtn.backgroundColor = UIColor.darkGray
}
}
I am curious if someone can help me out.
Thank you! :)

Your code is fine however it will work if only one cell is selected, because what you are essentially doing is changing the value of the Bool to true is selected then false if deselected without checking the total number of selected rows.
The easiest way without changing a lot of your code is to add a new variable called
var rowsSelected = 0
And in your didSelectRow add one to the rowsSelected += 1
Then in your if statement check if the value of
If rowsSelected is >= 1 {
//enable and un hide your button
}
On the deselectRow do the opposite and subtract 1 from rowsSelected -= 1 then the If statement if rowsSelected == 0 { //hide the button and disable it}
I hope this helps.

Here i have made a small change in your code. See if it helps you. Create a variable on the top like someRowSelected
var indexPAthSelected : NSIndexPath?
if !someRowSelected {
self.someRowSelected = true
nextBtn.isEnabled = true
nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
indexPAthSelected = indexPath
} else if indexPAthSelected == indexPath {
self.someRowSelected = false
nextBtn.isEnabled = false
nextBtn.backgroundColor = UIColor.darkGray
} else {
}

Related

Collection view cell displaying wrong background color in some views

The background color supposedly rendered on Card D was rendered in Card A in a different row. This is weird because the background color of the card title is set at the same time as the background colors of the views behind it, but only the views behind don't render the right color, while the view on top renders the right color.
Attached Screenshot-
Code from tableview:
let cell: BrowseReadTableViewCell = tableView.dequeueReusableCell(for: indexPath)
cell.update(properties: properties[indexPath.section]?)
cell.layoutIfNeeded()
return cell
Code from collectionview:
override func prepareForReuse() {
super.prepareForReuse()
typeBackground.backgroundColor = .clear
for bgView in bgCollection { //bgCollection is the collection of cards behind this are the views that don't render the right color
bgView.backgroundColor = UIColor.clear
}
}
func update(resource: Resource, properties: CatalogProperties?) {
if let cardColor = properties?.appearance?.cardHeader {
if let bgColor = cardColor.backgroundColor {
for bgView in bgCollection {
bgView.backgroundColor = UIColor(red: CGFloat(bgColor.rgb![0] / 255), green: CGFloat(bgColor.rgb![1] / 255), blue: CGFloat(bgColor.rgb![2] / 255), alpha: CGFloat(bgColor.alpha ?? 1))
}
typeBackground.backgroundColor = UIColor(red: CGFloat(bgColor.rgb![0] / 255), green: CGFloat(bgColor.rgb![1] / 255), blue: CGFloat(bgColor.rgb![2] / 255), alpha: CGFloat(bgColor.alpha ?? 1))
}
if let fgColor = cardColor.foregroundColor {
typeLabel.textColor = UIColor(red: CGFloat(fgColor.rgb![0] / 255), green: CGFloat(fgColor.rgb![1] / 255), blue: CGFloat(fgColor.rgb![2] / 255), alpha: CGFloat(fgColor.alpha ?? 1))
}
} else {
typeBackground.backgroundColor = UIColor.init(hexString: "#7C58C5")
for bgView in bgCollection {
bgView.backgroundColor = UIColor.init(hexString: "#7C58C5")
}
}

How do I resize the header of a uitableview?

I have a uitableview
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let width = tableView.frame.width - CGFloat((start*2))
let headerView = UIView()
headerView.frame.origin.x = 0
headerView.frame.origin.y = -45
headerView.frame.size.width = tableView.frame.width
let desc = UILabel()
desc.frame.origin.x = label.frame.origin.x
desc.frame.origin.y = line.frame.maxY + 10
desc.frame.size.width = width
desc.numberOfLines = 0
desc.font = UIFont(name: "Lato-Regular", size: 17);
desc.textColor = UIColor(red: 0.53, green: 0.53, blue: 0.53, alpha: 1.00)
desc.text = self.data.desc
desc.backgroundColor = UIColor.clear
desc.textAlignment = .left
desc.adjustsFontForContentSizeCategory = true
if (loadMoreDesc) {
desc.sizeToFit()
} else {
desc.frame.size.height = 110
}
headerView.addSubview(desc)
let moreButtonDesc = UIButton(frame: CGRect(x: desc.frame.minX, y: desc.frame.maxY, width: 80, height: 15))
moreButtonDesc.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
moreButtonDesc.titleLabel?.textAlignment = .left
if (loadMoreDesc) {
moreButtonDesc.setTitle("Less", for: .normal)
} else {
moreButtonDesc.setTitle("More", for: .normal)
}
moreButtonDesc.titleLabel?.font = UIFont(name: "Lato-Regular", size: 15);
moreButtonDesc.setTitleColor(UIColor(red: 0.00, green: 0.48, blue: 1.00, alpha: 1.00), for: .normal)
moreButtonDesc.addTarget(self, action:#selector(self.expandDesc(sender:)), for: .touchUpInside)
headerView.addSubview(moreButtonDesc)
When people click on "More" button the description should expand and the height of the table view header should increase to fit the description (desc). How can I accomplish this? Here's my expandDesc function
#objc func expandDesc(sender: UIButton) {
loadMoreDesc = !loadMoreDesc
tableView.reloadData()
}
Please note that the description is of dynamic height. I won’t know the height to set for the header unless I know the height of the description.
please can you try this
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if loadMoreDesc {
return 100
} else {
return 50.0
}
}

Swift Playground UITable View Cell not resizing after I add a subview

the table view loads
the sub view loads but you can see it is over other cells
sometimes the sub view loads but other cells are on top of it
I have a UITableView, and when the user touches a row a subview gets added to the screen. When they click another row, the last subview disappears (and the row should shrink back down to its normal size). The subview displays perfectly (and goes away when another row is clicked), but it hangs out of the row and covers some of the other options. Also, when the table view reloads after screen rotations, the subview ends up behind the other rows of the table.
I want the row with the subview to resize so that it matches the frame of the subview. Also, each subview is a different size depending on the text and images included in it. Because of this I have tried using auto layouts and UITableViewAutomaticDimension, but nothing seems to be working.
I am including my UITableViewController so you can see what I have and can steer me in the right direction.
class TableViewController : UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
self.tableView.estimatedRowHeight = rowHeight
return filmsToDisplay.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var currentFilm = filmsToDisplay[indexPath.row]
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.selectionStyle = .none
cell.textLabel?.text = "\(currentFilm.year) - \(currentFilm.movieTitle)"
cell.textLabel?.textColor = #colorLiteral(red: 0.254901975393295, green: 0.274509817361832, blue: 0.301960796117783, alpha: 1.0)
cell.textLabel?.font = UIFont.systemFont(ofSize: fontSize)
cell.textLabel?.sizeToFit()
cell.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
cell.contentView.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
let accessory = makeChart(currentFilm: filmsToDisplay[indexPath.row])
cell.contentView.addSubview(accessory)
accessory.translatesAutoresizingMaskIntoConstraints = false
accessory.trailingAnchor.constraint(equalTo:cell.contentView.trailingAnchor).isActive = true
accessory.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
accessory.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
accessory.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: nomBarWidth).isActive = true
cell.contentView.translatesAutoresizingMaskIntoConstraints=false
cell.contentView.leadingAnchor.constraint(equalTo:cell.leadingAnchor).isActive = true
cell.contentView.trailingAnchor.constraint(equalTo:cell.trailingAnchor).isActive = true
cell.contentView.topAnchor.constraint(equalTo:cell.topAnchor).isActive = true
cell.contentView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
return cell
}
var lastSelectedCell : UITableViewCell? = nil
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentFilm = filmsToDisplay[indexPath.row]
var detailView = createView(film: currentFilm)
detailView.tag = 555
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
//get rid of last detail view
if let lastCell = lastSelectedCell {
for i in 0..<lastCell.contentView.subviews.count {
let content = lastCell.contentView.subviews[i]
if content.tag == 555{
content.removeFromSuperview()
}
}
}
selectedCell.contentView.addSubview(detailView)
detailView.translatesAutoresizingMaskIntoConstraints=false
detailView.leadingAnchor.constraint(equalTo:selectedCell.contentView.leadingAnchor).isActive = true
detailView.trailingAnchor.constraint(equalTo:selectedCell.contentView.trailingAnchor).isActive = true
detailView.topAnchor.constraint(equalTo:selectedCell.contentView.topAnchor).isActive = true
detailView.bottomAnchor.constraint(equalTo:detailView.subviews[detailView.subviews.count - 2].bottomAnchor).isActive = true
lastSelectedCell = selectedCell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
The pieces of the sub view define their height as follows (there are more pieces but they all have the same kind of constraints:
//MOVIE TITLE BAR
let movieTitleBar = UIButton()
movieTitleBar.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: UIControlState.normal)
movieTitleBar.setTitle(film.movieTitle, for: UIControlState.normal)
movieTitleBar.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
movieTitleBar.alpha = 0.5
movieTitleBar.layer.borderWidth = outlineWidth
movieTitleBar.layer.borderColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
}
filmView.addSubview(movieTitleBar)
movieTitleBar.translatesAutoresizingMaskIntoConstraints = false
movieTitleBar.topAnchor.constraint(equalTo:filmView.topAnchor).isActive = true
movieTitleBar.heightAnchor.constraint(equalToConstant: rowHeight).isActive = true
movieTitleBar.leadingAnchor.constraint(equalTo:filmView.leadingAnchor).isActive = true
movieTitleBar.trailingAnchor.constraint(equalTo:filmView.trailingAnchor).isActive = true
//POSTER IMAGE
var poster = UIButton()
isPoster = false
if let posterImg = film.image {
if let url = NSURL(string:posterImg){
if let data = NSData(contentsOf:url as URL){
film.poster = UIImage(data:data as Data)
poster.setImage(film.poster, for: UIControlState.normal)
isPoster = true
filmView.addSubview(poster)
}
}
}
//BRIEF DESCRIPTION LABEL
let briefDescriptionBar = UILabel()
briefDescriptionBar.backgroundColor = film.colorUI
briefDescriptionBar.alpha = 0.8
briefDescriptionBar.text = "\(film.year) - \(film.briefDescription)"
briefDescriptionBar.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
briefDescriptionBar.numberOfLines = 0
briefDescriptionBar.textAlignment = .center
filmView.addSubview(briefDescriptionBar)
briefDescriptionBar.translatesAutoresizingMaskIntoConstraints = false
briefDescriptionBar.leadingAnchor.constraint(equalTo:movieTitleBar.leadingAnchor).isActive = true
briefDescriptionBar.topAnchor.constraint(equalTo:movieTitleBar.bottomAnchor).isActive = true
if isPoster == true {
poster.translatesAutoresizingMaskIntoConstraints = false
poster.topAnchor.constraint(equalTo:movieTitleBar.bottomAnchor).isActive = true
poster.trailingAnchor.constraint(equalTo:movieTitleBar.trailingAnchor).isActive = true
poster.widthAnchor.constraint(equalTo:filmView.widthAnchor, multiplier:nomBarWidth).isActive = true
let aspect = (nomBarWidth * 41) / 27
poster.heightAnchor.constraint(equalTo: filmView.widthAnchor, multiplier: aspect).isActive = true
briefDescriptionBar.trailingAnchor.constraint(equalTo:poster.leadingAnchor).isActive = true
briefDescriptionBar.bottomAnchor.constraint(equalTo:poster.bottomAnchor).isActive = true
}
else {
briefDescriptionBar.widthAnchor.constraint(equalTo:filmView.widthAnchor).isActive = true
briefDescriptionBar.heightAnchor.constraint(equalTo:movieTitleBar.heightAnchor, multiplier : 2).isActive = true
}
Your problem is that you don't give a height constraint for the subviews you add , you only hook leading ,trailing , top and bottom but the contentView expects it's height from inner subviews ( if they don't have intrinsic content size like labels / buttons ) , also no need for this part of code
cell.contentView.translatesAutoresizingMaskIntoConstraints=false
cell.contentView.leadingAnchor.constraint(equalTo:cell.leadingAnchor).isActive = true
cell.contentView.trailingAnchor.constraint(equalTo:cell.trailingAnchor).isActive = true
cell.contentView.topAnchor.constraint(equalTo:cell.topAnchor).isActive = true
cell.contentView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
Also there should be only one view inside to the contentView that is connected to the bottom , if you add more than one , then you'll get conflicts if both views are also hooked to top of it , so you have to break last view bottom constraint before hooking a new one

How to make cells pre-selected when app became launched?

I have typical table view with opportunity to be selected. I want to make them selected as default.
import UIKit
class WeekdaysViewController: UITableViewController {
var weekdays: [Int]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
view.backgroundColor = UIColor.white
weekdays = [1, 2, 3, 4, 5, 6, 7]
}
override func viewWillDisappear(_ animated: Bool) {
performSegue(withIdentifier: Id.weekdaysUnwindIdentifier, sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
for weekday in weekdays {
if weekday == (indexPath.row + 1) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
if let index = weekdays.index(of: (indexPath.row + 1)){
weekdays.remove(at: index)
cell.setSelected(true, animated: true)
cell.setSelected(false, animated: true)
cell.accessoryType = UITableViewCellAccessoryType.none
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
//cell.backgroundColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
//cell.tintColor = UIColor.white
} else {
//row index start from 0, weekdays index start from 1 (Sunday), so plus 1
weekdays.append(indexPath.row + 1)
cell.setSelected(true, animated: true)
cell.setSelected(false, animated: true)
cell.accessoryType = UITableViewCellAccessoryType.checkmark
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
//cell.backgroundColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
//cell.tintColor = UIColor.white
}
}
}
extension WeekdaysViewController {
static func repeatText( weekdays: [Int]) -> String {
if weekdays.count == 7 {
return "Every Day"
}
if weekdays.isEmpty {
return "No repeat"
}
let preferredLanguage = Locale.current.languageCode
let locale = Locale.current.regionCode
var ret = String()
var weekdaysSorted:[Int] = [Int]()
weekdaysSorted = weekdays.sorted(by: <)
if preferredLanguage == "ru" {
for day in weekdaysSorted {
switch day {
case 1:
ret += "Sun "
case 2:
ret += "Mon "
case 3:
ret += "Tue "
case 4:
ret += "Wed "
case 5:
ret += "Thu "
case 6:
ret += "Fri "
case 7:
ret += "Sat "
default:
//throw
break
}
}
} else {
for day in weekdaysSorted {
switch day{
case 1:
ret += "Sun "
case 2:
ret += "Mon "
case 3:
ret += "Tue "
case 4:
ret += "Wed "
case 5:
ret += "Thu "
case 6:
ret += "Fri "
case 7:
ret += "Sat "
default:
//throw
break
}
}
}
return ret
}
}
I've added in viewDidLoad()
weekdays = [1, 2, 3, 4, 5, 6, 7]
It's almost working but only when I'm moving to WeekdaysViewController.
How to resolve that problem?
Use this snippet to automatically select your table row index.
Swift 3
tableView.selectRow(at: IndexPath(item: 1, section: 0), animated: true, scrollPosition: .middle)
Sample Image

Views from UITableView disappearing

I have a custom cell that is showing 14 different views. Depending on what data the cell is receiving it should show the views that are equal to the .count of some data, and the color should change depending on what data it is.
For instance:
If it is receiving three types of data, it should only show 3 views. Could be ice cream, candy, marshmellows. Then it would show three views, in orange (ice cream), blue (candy), green (marshmellows).
I got that working pretty well and I am excited about it. The problem is, if I have a cell showing three views and scroll down to a cell only containing 1 view (because the data was only one), then when I scroll up to the first cell again that should originally show three views, it is only showing 1, the first one..
I have an example:
My custom cell in storyboard is like this, the green and black boxes are the different views
This is what it looks like with 6 types of data:
When I then scroll down to a cell containing one view, the cell with 6 views will look like this afterwards:
Here is some relevant code:
Let me explain the code. In my database, every post has a category which is either 1 or 2. That is what the code is searching for, in update.category. If it is category 1, then it is just plain text. If it is category 2 ( or something else ) it should show the views, so I actually have to types of cells in my UITableViewController.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let update = updates[indexPath.row]
if update.category == 1 {
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
cell.nameButton.setTitle(update.addedByUser, forState: .Normal)
return cell
} else {
let cellSugar:newSugarTableViewCell = tableView.dequeueReusableCellWithIdentifier("CellSugar", forIndexPath: indexPath) as! newSugarTableViewCell
cellSugar.nameButton.setTitle(update.addedByUser, forState: .Normal)
let sugarArray = update.content.componentsSeparatedByString("--")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cellSugar.sugarViewOne.layer.cornerRadius = cellSugar.sugarViewOne.frame.size.width/2
cellSugar.sugarViewOne.clipsToBounds = true
cellSugar.sugarViewOne.layer.borderColor = UIColor.whiteColor().CGColor
cellSugar.sugarViewOne.layer.borderWidth = 2.0
cellSugar.sugarViewTwo.layer.cornerRadius = cellSugar.sugarViewTwo.frame.size.width/2
cellSugar.sugarViewTwo.clipsToBounds = true
cellSugar.sugarViewTwo.layer.borderColor = UIColor.whiteColor().CGColor
cellSugar.sugarViewTwo.layer.borderWidth = 2.0
})
if sugarArray.count == 1 {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#")
if seperateSugarArray[4] == "Candy" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå
} else if seperateSugarArray[4] == "Ice cream" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå
} else if seperateSugarArray[4] == "Marshmellows" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis
}
cellSugar.sugarViewTwo.hidden = true
cellSugar.sugarViewThree.hidden = true
cellSugar.sugarViewFour.hidden = true
cellSugar.sugarViewFive.hidden = true
cellSugar.sugarViewSix.hidden = true
cellSugar.sugarViewSeven.hidden = true
cellSugar.sugarViewEight.hidden = true
cellSugar.sugarViewNine.hidden = true
cellSugar.sugarViewTen.hidden = true
cellSugar.sugarViewEleven.hidden = true
cellSugar.sugarViewTwelve.hidden = true
cellSugar.sugarViewThirteen.hidden = true
cellSugar.sugarViewFourteen.hidden = true
})
} else if sugarArray.count == 2 {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#")
let seperateSugarArrayTwo = sugarArray[1].componentsSeparatedByString("#")
if seperateSugarArray[4] == "Candy" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå
} else if seperateSugarArray[4] == "Ice cream" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå
} else if seperateSugarArray[4] == "Marshmellows" {
cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis
}
if seperateSugarArray[4] == "Candy" {
cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå
} else if seperateSugarArray[4] == "Ice cream" {
cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå
} else if seperateSugarArray[4] == "Marshmellows" {
cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis
}
cellSugar.sugarViewThree.hidden = true
cellSugar.sugarViewFour.hidden = true
cellSugar.sugarViewFive.hidden = true
cellSugar.sugarViewSix.hidden = true
cellSugar.sugarViewSeven.hidden = true
cellSugar.sugarViewEight.hidden = true
cellSugar.sugarViewNine.hidden = true
cellSugar.sugarViewTen.hidden = true
cellSugar.sugarViewEleven.hidden = true
cellSugar.sugarViewTwelve.hidden = true
cellSugar.sugarViewThirteen.hidden = true
cellSugar.sugarViewFourteen.hidden = true
})
}
return cellSugar
}
}
I hope you understand and that you can help me, as it is pretty annoying :-)
You have to update all elements for each condition.
the cause of your issue: the table view is reusing the cells so if you have a cell showing 5 labels and after scrolling you have another one has a 3 and the other 2 is hidden, when you scroll up again you have to make them visible again a table view is using the one with 2 hidden labels.
In CellForRowAtIndexPath add the missing labels in each conditions
or in your custom UItableViewCell class add the method prepareForReuseand make all the labels visible.
UITableView reuse cells in a way that it takes one cell in its current state (with all its views configured) and return that cell for an other row, for which you need to again configure the cell as you need.
if update.category == 1 {
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
cell.nameButton.setTitle(update.addedByUser, forState: .Normal)
return cell
}
You need to configure your cell for a current row data before returning it.

Resources