Views from UITableView disappearing - ios

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.

Related

iOS Eureka: How to select first value as default after lazy get

I have a list retrieved from REST API to Eureka's PushRow, but in case none is selected (creating a new object) I want first option to be selected by default after option list is retrieved.
However, PushRow only loads list after it's clicked on.
So is there a way to load PushRow lazy options together with it's section, or should I switch to other type of row (if it support's lazy)
You can use
$0.value // To set default value for PushRow()
Check that value is there before set to $0.value
Here what i used
<<< PushRow<String>(){
$0.tag = String(index)
$0.selectorTitle = checklistFieldNames[index] //
$0.options = ["YES", "NO"]
if question.count == 0{
$0.value = ""
}else {
$0.value = checklistFieldAnswers[0]
}
}.onPresent({ (from, to) in
to.dismissOnChange = false
to.dismissOnSelection = true
to.tableView?.backgroundColor = UIColor(displayP3Red: 244/255, green: 244/255, blue: 244/255, alpha: 1.0)
to.view.backgroundColor = UIColor(displayP3Red: 244/255, green: 244/255, blue: 244/255, alpha: 1.0)
to.selectableRowCellUpdate = { cell, row in
cell.textLabel?.font = UIFont(name: "DTLProkyonT", size: 15)
cell.textLabel?.textColor = UIColor.lightGray
cell.textLabel?.numberOfLines = 0
cell.textLabel?.backgroundColor = UIColor.clear
}
})

UITableView Background Color Different From UITableViewCell

I am setting the tableView background color the same as the tableViewCell background color, but the colors look different. The colors look the same when set to a custom color but when there is no custom color set they are different. isColorSet is 0 on start so both should be set to UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0). How can I get the colors to look the same?
Setting tableView background color:
override func viewWillAppear(_ animated: Bool) {
//Set background color
if userDefaults.integer(forKey: "isColorSet") == 0 {
navigationController?.navigationBar.barTintColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
tableView.backgroundColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
} else if userDefaults.integer(forKey: "isColorSet") == 1 {
let red = CGFloat(userDefaults.float(forKey: "red"))
let green = CGFloat(userDefaults.float(forKey: "green"))
let blue = CGFloat(userDefaults.float(forKey: "blue"))
navigationController?.navigationBar.barTintColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
tableView.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
}
}
Setting tableViewCell background color:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ideaCell") as! IdeaTableViewCell
cell.ideaTitleLabel.text = ideas[indexPath.section][indexPath.row].title
cell.ideaDescLabel.text = ideas[indexPath.section][indexPath.row].desc
//Set date
let date = userDefaults.object(forKey: "date.\(indexPath.section).\(indexPath.row)") as! Date
let month = Calendar.current.component(.month, from: date)
let day = Calendar.current.component(.day, from: date)
let year = Calendar.current.component(.year, from: date)
if userDefaults.string(forKey: "dateStyle") == "MDY" {
cell.ideaDateLabel.text = "\(month)/\(day)/\(year)"
} else if userDefaults.string(forKey: "dateStyle") == "DMY" {
cell.ideaDateLabel.text = "\(day)/\(month)/\(year)"
} else if userDefaults.string(forKey: "dateStyle") == "YMD" {
cell.ideaDateLabel.text = "\(year)/\(month)/\(day)"
}
//Set color
if userDefaults.integer(forKey: "isColorSet") == 0 {
cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)
} else if userDefaults.integer(forKey: "isColorSet") == 1 {
let red = CGFloat(userDefaults.float(forKey: "red"))
let green = CGFloat(userDefaults.float(forKey: "green"))
let blue = CGFloat(userDefaults.float(forKey: "blue"))
cell.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
}
return cell
Setting rgb colors for UserDefaults:
func HSBColorColorPickerTouched(sender: HSBColorPicker, color: UIColor, point: CGPoint, state: UIGestureRecognizer.State) {
setButton.backgroundColor = color
red = color.rgb()?.red
green = color.rgb()?.green
blue = color.rgb()?.blue
redLabel.text = "Red: \(red!)"
redSlider.value = Float(red)
greenLabel.text = "Green: \(green!)"
greenSlider.value = Float(green)
blueLabel.text = "Blue: \(blue!)"
blueSlider.value = Float(blue)
}
#IBAction func sliderValueChanged(_ sender: Any) {
red = redSlider.value
green = greenSlider.value
blue = blueSlider.value
if red != nil {
redLabel.text = "Red: \(red!)"
}
if green != nil {
greenLabel.text = "Green: \(green!)"
}
if blue != nil {
blueLabel.text = "Blue: \(blue!)"
}
setButton.backgroundColor = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1.0)
}
#IBAction func setColor(_ sender: Any) {
userDefaults.removeObject(forKey: "red")
userDefaults.removeObject(forKey: "green")
userDefaults.removeObject(forKey: "blue")
userDefaults.set(red, forKey: "red")
userDefaults.set(green, forKey: "green")
userDefaults.set(blue, forKey: "blue")
if userDefaults.float(forKey: "isColorSet") == 0 {
userDefaults.set(1, forKey: "isColorSet")
}
_ = navigationController?.popToRootViewController(animated: true)
}
Screenshot with isColorSet = 0
Screenshot with isColorSet = 1
Screenshot of Color Picker
The issue is a simple typo in the following line:
cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)
Your denominator should be 255 for the green and blue values.

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

Loading remote image with DTAttributeTextContentView

I am developing an application for an online forum with DTCoreText to display thread replies that parsed from BBCode to HTML with text and images inside custom cells in a UITableView. I use interface builder to build my prototype cell and link it to my custom cell class using outlets. Texts are displaying correctly but the images are not loading and leaves a empty space inside the cell view. Heres my view controller and cell class code
My View Controller:
//
import UIKit
import NVActivityIndicatorView
class ContentViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//MARK: Properties
var threadIdReceived: String = ""
var channelNow: String = ""
var isRated: Bool = false
var pageNow: Int = 1
var convertedText: String = ""
var op = OP(t: "",n: "",l: "",c: "",cH: "",a: "",d: "",gd: "",b: "",ge: "",ch: "")
var comments = [Replies]()
var replyCount = 1
#IBOutlet weak var contentTableView: UITableView!
#IBOutlet weak var pageButton: UIBarButtonItem!
#IBOutlet weak var goodCount: UIBarButtonItem!
#IBOutlet weak var badCount: UIBarButtonItem!
#IBOutlet weak var prevButton: UIBarButtonItem!
#IBOutlet weak var nextButton: UIBarButtonItem!
let backgroundIndicator = NVActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height),type: .ballPulseSync,padding: 175)
//HKGalden API (NOT included in GitHub repo)
var api = HKGaldenAPI()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.isTranslucent = true
contentTableView.delegate = self
contentTableView.dataSource = self
backgroundIndicator.startAnimating()
self.view.addSubview(backgroundIndicator)
self.prevButton.isEnabled = false
self.pageButton.title = "第" + String(pageNow) + "頁"
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
contentTableView.isHidden = true
api.fetchContent(postId: threadIdReceived, pageNo: String(pageNow), completion: {
[weak self] op,comments,error in
if (error == nil) {
self?.op = op
self?.comments = comments
self?.title = op.title
self?.goodCount.title = op.good
self?.badCount.title = op.bad
self?.contentTableView.reloadData()
self?.backgroundIndicator.isHidden = true
self?.contentTableView.isHidden = false
self?.contentTableView.tableFooterView = UIView()
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (comments.count) + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell", for: indexPath) as! ContentTableViewCell
if(indexPath.row == 0) {
cell.configureOP(opData: op)
}
else {
cell.configureReply(comments: comments, indexPath: indexPath,pageNow: pageNow)
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
#IBAction func changePage(_ sender: UIBarButtonItem) {
switch (sender.tag) {
case 0:
contentTableView.isHidden = true
backgroundIndicator.isHidden = false
self.pageNow -= 1
self.pageButton.title = "第" + String(self.pageNow) + "頁"
self.api.fetchContent(postId: threadIdReceived, pageNo: String(pageNow), completion: {
[weak self] op,comments,error in
if (error == nil) {
self?.op = op
self?.comments = comments
self?.title = op.title
self?.goodCount.title = op.good
self?.badCount.title = op.bad
self?.contentTableView.reloadData()
self?.backgroundIndicator.isHidden = true
self?.contentTableView.isHidden = false
self?.contentTableView.tableFooterView = UIView()
}
})
case 1:
self.prevButton.isEnabled = true
contentTableView.isHidden = true
backgroundIndicator.isHidden = false
self.pageNow += 1
self.pageButton.title = "第" + String(self.pageNow) + "頁"
self.api.fetchContent(postId: threadIdReceived, pageNo: String(pageNow), completion: {
[weak self] op,comments,error in
if (error == nil) {
self?.op = op
self?.comments = comments
self?.title = op.title
self?.goodCount.title = op.good
self?.badCount.title = op.bad
self?.contentTableView.reloadData()
self?.backgroundIndicator.isHidden = true
self?.contentTableView.isHidden = false
self?.contentTableView.tableFooterView = UIView()
}
})
default:
print("nothing")
}
}
//MARK: Private Functions
private func barTintColorChanger(channelId: String) {
switch channelId {
case "bw":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 72/255, green: 125/255, blue: 174/255, alpha: 1)
case "et":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 152/255, green: 85/255, blue: 159/255, alpha: 1)
case "ca":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 33/255, green: 136/255, blue: 101/255, alpha: 1)
case "fn":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 33/255, green: 136/255, blue: 101/255, alpha: 1)
case "gm":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 37/255, green: 124/255, blue: 201/255, alpha: 1)
case "ap":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 41/255, green: 145/255, blue: 185/255, alpha: 1)
case "it":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255, green: 106/255, blue: 125/255, alpha: 1)
case "mp":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 88/255, green: 100/255, blue: 174/255, alpha: 1)
case "sp":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 152/255, green: 64/255, blue: 81/255, alpha: 1)
case "lv":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 196/255, green: 53/255, blue: 94/255, alpha: 1)
case "sy":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 127/255, green: 112/255, blue: 106/255, alpha: 1)
case "ed":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 65/255, green: 143/255, blue: 66/255, alpha: 1)
case "tm":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 190/255, green: 71/255, blue: 30/255, alpha: 1)
case "tr":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 97/255, green: 118/255, blue: 83/255, alpha: 1)
case "an":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 171/255, green: 80/255, blue: 159/255, alpha: 1)
case "to":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 148/255, green: 95/255, blue: 50/255, alpha: 1)
case "mu":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 88/255, green: 95/255, blue: 202/255, alpha: 1)
case "vi":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 121/255, green: 78/255, blue: 126/255, alpha: 1)
case "dc":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 56/255, green: 102/255, blue: 118/255, alpha: 1)
case "st":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 128/255, green: 113/255, blue: 143/255, alpha: 1)
case "ts":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 172/255, green: 48/255, blue: 66/255, alpha: 1)
case "mb":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 124/255, green: 89/255, blue: 196/255, alpha: 1)
case "ia":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 132/255, green: 169/255, blue: 64/255, alpha: 1)
case "ac":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 150/255, green: 75/255, blue: 112/255, alpha: 1)
case "ep":
self.navigationController?.navigationBar.barTintColor = UIColor(red: 152/255, green: 112/255, blue: 93/255, alpha: 1)
default:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1)
}
}
}
And heres my cell class:
import UIKit
import Kingfisher
import DTCoreText
class ContentTableViewCell: UITableViewCell, DTAttributedTextContentViewDelegate, DTLazyImageViewDelegate {
//MARK: Properties
#IBOutlet weak var userNameLabel: UILabel!
#IBOutlet weak var userLevelLabel: UILabel!
#IBOutlet weak var userAvatarImageView: UIImageView!
#IBOutlet weak var replyCountLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var contentTextView: DTAttributedTextContentView!
let attributedOptions: NSDictionary = [
NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentAttributeKey.characterEncoding: String.Encoding.utf8,
DTDefaultFontFamily: "Helvetica Neue",
DTDefaultFontSize: 15,
DTMaxImageSize: CGSize(width:280,height:450)
]
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
contentTextView.shouldDrawImages = false
contentTextView.delegate = self as DTAttributedTextContentViewDelegate
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func attributedTextContentView(_ attributedTextContentView: DTAttributedTextContentView!, viewFor attachment: DTTextAttachment!, frame: CGRect) -> UIView! {
if attachment is DTImageTextAttachment {
let imageView = DTLazyImageView(frame: frame)
imageView.contentView = contentTextView
imageView.delegate = self as DTLazyImageViewDelegate
//url for deferred loading
imageView.url = attachment.contentURL
imageView.shouldShowProgressiveDownload = true
return imageView
}
return nil
}
func lazyImageView(_ lazyImageView: DTLazyImageView!, didChangeImageSize size: CGSize) {
let url = lazyImageView.url
let pred = NSPredicate(format: "contentURL == %#",url! as CVarArg)
let array = self.contentTextView.layoutFrame.textAttachments(with: pred)
for _ in (array?.enumerated())! {
let element = DTTextAttachment()
element.originalSize = size
element.displaySize = size
}
contentTextView.layouter = nil
contentTextView.relayoutText()
}
func configureOP(opData: OP) {
userNameLabel.text = opData.name
if(opData.gender == "male") {
userNameLabel.textColor = UIColor(red:0.68, green:0.78, blue:0.81, alpha:1.0)
}
else {
userNameLabel.textColor = UIColor(red:1.00, green:0.41, blue:0.38, alpha:1.0)
}
if(opData.level == "lv1") {
userLevelLabel.text = "普通會然"
userLevelLabel.backgroundColor = UIColor.darkGray
}
else if (opData.level == "lv2") {
userLevelLabel.text = "迷の存在"
userLevelLabel.backgroundColor = UIColor(red:0.55, green:0.00, blue:0.00, alpha:1.0)
}
else if (opData.level == "lv3") {
userLevelLabel.text = "肉務腸"
userLevelLabel.backgroundColor = UIColor(red:0.47, green:0.87, blue:0.47, alpha:1.0)
}
else if (opData.level == "lv4") {
userLevelLabel.text = "唉屎"
userLevelLabel.backgroundColor = UIColor(red:0.00, green:1.00, blue:1.00, alpha:1.0)
}
let avatarURL = URL(string: "https://hkgalden.com" + opData.avatar)
if (opData.avatar == "") {
userAvatarImageView.image = UIImage(named: "DefaultAvatar")
}
else {
userAvatarImageView.kf.setImage(with: avatarURL)
}
//remove abundant </img> tag
opData.contentHTML = opData.contentHTML.replacingOccurrences(of: "</img>", with: "")
//print(opData.contentHTML)
let contentData = opData.contentHTML.data(using: .utf8)
let contentAttr = NSAttributedString.init(htmlData: contentData, options: attributedOptions as! [AnyHashable : Any], documentAttributes: nil)
//print(contentAttr)
contentTextView.attributedString = contentAttr
replyCountLabel.text = "OP"
dateLabel.text = opData.date
}
func configureReply(comments: [Replies],indexPath: IndexPath,pageNow: Int) {
userNameLabel.text = comments[indexPath.row - 1].name
if(comments[indexPath.row - 1].gender == "male") {
userNameLabel.textColor = UIColor(red:0.68, green:0.78, blue:0.81, alpha:1.0)
}
else {
userNameLabel.textColor = UIColor(red:1.00, green:0.41, blue:0.38, alpha:1.0)
}
if(comments[indexPath.row - 1].level == "lv1") {
userLevelLabel.text = "普通會然"
userLevelLabel.backgroundColor = UIColor.darkGray
}
else if (comments[indexPath.row - 1].level == "lv2") {
userLevelLabel.text = "迷の存在"
userLevelLabel.backgroundColor = UIColor(red:0.55, green:0.00, blue:0.00, alpha:1.0)
}
else if (comments[indexPath.row - 1].level == "lv3") {
userLevelLabel.text = "肉務腸"
userLevelLabel.backgroundColor = UIColor(red:0.47, green:0.87, blue:0.47, alpha:1.0)
}
else if (comments[indexPath.row - 1].level == "lv4") {
userLevelLabel.text = "唉屎"
userLevelLabel.backgroundColor = UIColor(red:0.00, green:1.00, blue:1.00, alpha:1.0)
}
let avatarURL = URL(string: "https://hkgalden.com" + comments[indexPath.row - 1].avatar)
if (comments[indexPath.row - 1].avatar == "") {
userAvatarImageView.image = UIImage(named: "DefaultAvatar")
}
else {
userAvatarImageView.kf.setImage(with: avatarURL)
}
replyCountLabel.text = "#" + String(25 * (pageNow - 1) + indexPath.row)
dateLabel.text = comments[indexPath.row - 1].date
let contentData = comments[indexPath.row - 1].contentHTML.data(using: .utf8)
let contentAttr = NSAttributedString.init(htmlData: contentData, options: attributedOptions as! [AnyHashable : Any], documentAttributes: nil)
//print(contentAttr)
contentTextView.attributedString = contentAttr
}
}
Try this category - it works for me
extension UIImageView {
public func imageFromUrl(url: URL?) {
if let url = url {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async { [weak self] in
self?.image = UIImage(data: data)
}
}
task.resume()
}
}
}

Change button state if a row in tableView is selected

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 {
}

Resources