Object in tableViewCell are shown only when cell selected - ios

I have custom UITableViewCell, which has button and progress view. It has 5 progress view objects.
In viewController.swift I have a struct where 6 variables (one of type button and five of type progress view) are.
When I'm clicking this button, I'm setting struct variables to these five progress view objects. Also NSUrlSession function starts which downloading file. Downloading progress must be shown in these progress views. And it working properly except moment that to see these progress view I must focus, select this cell. How can I fix it so by clicking this button all progress view will be shown in cell without its selecting?
Some code:
struct vis {
static var b: UIButton!
static var p1: UIProgressView!
static var p2: UIProgressView!
static var p3: UIProgressView!
static var p4: UIProgressView!
static var p5: UIProgressView!
}
vis.b = (sender as? UIButton)!
vis.p1 = (cell?.viewWithTag(5) as? UIProgressView)!
vis.p2 = (cell?.viewWithTag(6) as? UIProgressView)!
vis.p3 = (cell?.viewWithTag(7) as? UIProgressView)!
vis.p4 = (cell?.viewWithTag(8) as? UIProgressView)!
vis.p5 = (cell?.viewWithTag(9) as? UIProgressView)!

You can disable cell selection by tableView.allowsSelection = NO; and then you can use progressTintColor and trackTintColor or progressImage and trackImage to manage the colors for your progress controls.

Related

How to change button programmatically without outlet connection in swift

I have a list of buttons inside an array. I want to replace the buttons position order using that array. I can do this in java, but cant do the same in swift. Here is my code:
#IBOutlet weak var menuView: UIView!
struct ButtonsIndex {
var button: UIButton
var index: Int
}
var footer: [ButtonsIndex] = []
//Set Buttons order etc
...
func setFooter(){
let count = 0
for buttons in menuView.subviews {
if var button = buttons as? UIButton {
button = footer[count].button
count += 1
}
}
}
Is it impossible to change the buttons position without connecting the outlets to my code? Thanks in advance.

Swift 3 ios: UILabel needs to appear on top of a UIImage when the Image appears

So I am trying to give the user the ability to design their own virtual business card where the user can drag a label around the UIView, they can change background color of the UIView, as well as the color of the UILabel. The card itself is represented by a nib and the user selects the different color choices or template image choices through a UIPickerView. I want the Label to appear on top of the Image when it appears and is dragged to that part of the View, however it is not currently doing that. Changing colors of the background for the UIView and the text work just fine; I am also able to add and remove the Template image (which is a PNG) by using .isHidden().
My Problem is that when the UIImage is added to the UIView it is on top of the UILabel. If part of the UILabel overlaps with part of the png image it disappears behind it, and I would like it to appear over the image.
The code for the nib is in userCardView.swift and the picker view is on the mainViewController where I set the UserDefaults which works fine.
class userCardView: UIView {
#IBOutlet var cardView: UIView!
#IBOutlet weak var testLBL: UILabel!
#IBOutlet weak var templateImage: UIImageView!
var colorIndex = ["Black", "White", "Gray"]
var templateIndex = ["None", "Triangle"]
override func awakeFromNib() {
super.awakeFromNib()
cardView.clipToBounds = true
let backColorObject = UserDefaults.standard.object(forKey: "backGroundColor") as? String
let textColorObject = UserDefaults.standard.object(forKey: "textColor") as? String
let templateColorObject = UserDefaults.standard.object(forKey: "templateColor") as? String
let templateImageObject = UserDefaults.standard.object(forKey: "templateImage") as? String
for _ in templateIndex {
if templateImageObject == templateIndex[1] {
templateImage.isHidden = false
templateImage.image = UIImage(named: "test_triangle.png")
//testLBL.bringSubview(toFront: templateImage)
templateImage.bringSubview(toFront: testLBL)
} else if templateImageObject == templateIndex[0] {
templateImage.isHidden = true
} else {
templateImage.isHidden = true
}
}
I'm Still pretty new to using swift so any help at all would be greatly appreciated, thank you.
You need to ask testLBL's superview to bring it to the front. Your code templateImage.bringSubview(toFront: testLBL) won't work if templateImage is not the parent view of testLBL because testLBL is not in templateImage's view hierarchy.
So try to use the superview of testLBL
<testLBL's parent view>.bringSubview(toFront: testLBL)
or
testLBL.parentView.bringSubview(toFront: testLBL)
Hope it solves your problem

How can I make a UIImage View clickable to segue to another view controller

I'm new in programming and I'm stuck with this little problem. I created a table view with several items, that passes data to a label and an image view through a segue. It all works fine, but now I want to make the image clickable, in order to segue to another view controller to show this image expanded. How can I do that?
class ViewController: UIViewController {
#IBOutlet var titleLabel: UILabel!
#IBOutlet var descLabel: UILabel!
#IBOutlet var imageView: UIImageView!
var titleName: String?
var descName: String?
var imageName: String?
func configureView() {
if let poster = self.imageName {
if let imagePoster = self.imageView {
imagePoster.image = UIImage (named: poster)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = titleName
descLabel.text = descName
self.configureView()
}
The easiest thing to do is to use a UIButton instead of an image view. Set it's type to custom and install the image into the button in IB (Interface Builder).
That way you can trigger an IBAction just like any other button. It also highlights on a touch like you'd expect, triggers on touch up rather than touch down, etc.
If you don't want to use a button-with-image, you have to set userInteractionEnabled = YES on the image view and install a tap gesture recognizer on it. See the docs on UITapGestureRecognizer for more information.

Working UIButton animations in Swift

I've been trying to get this snippit of code to work but I can't seem to get my UIButton to animate through images.
#IBOutlet weak var screenButton: UIButton!;
func animation(){
var animationButton : UIButton!;
var imageList = [UIImage]();
var image1:UIImage = UIImage(named:"3fingers-o-l-animation1.png")!;
var image2:UIImage = UIImage(named:"4fingers-o-l-animation1.png")!;
animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList;
animationButton!.imageView!.animationDuration = 1.0;
animationButton!.imageView!.startAnimating();
}
After writing this code I've dragged a line from screenButton to the button I want to animate through the image in the Storyboard.
What am I doing wrong?
What you need to do is to not re-initialize the UIButton in the function animation; so, comment out this line:
//var animationButton : UIButton!
Besides just having an IBOutlet, we need also an IBAction to your function animation. Do the same thing as you do for your button by control dragging a "line" to the block of function (check IBAction, not IBOutlet).
A little walk-through while you are still in storyboard:
What you have is only one button named: animationButton, which is an IBOutlet from your storyboard to your code by control-dragging a "line".
For your animation function:
Control-drag also from storyboard from the button a "line" to the block of the animation function.
Set up an initial image for you button in storyboard as will be mentioned shortly below.
In addition, we need to set up an initial image in storyboard; otherwise, the image property of the button is nil.
Please do so in storyboard by clicking on the right hand side "Attributes Inspector" and then set an image where it says "image". As test, I set that up as "1.jpg".
Thus, you code should look like the following:
#IBOutlet weak var animationButton: UIButton!
#IBAction func animation()
{
print("button touched")
//var animationButton : UIButton!
var imageList = [UIImage]()
var image1:UIImage = UIImage(named:"1.jpg")!
var image2:UIImage = UIImage(named:"2.jpg")!
//animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList
animationButton!.imageView!.animationDuration = 1.0
animationButton!.imageView!.startAnimating()
}
Just ran a quick test; it gets touch event and the images animate on the button. Hope this helps.

Saving CoreData in UITableViewController but UISlider data is only in the Prototype Cell - how do I pull this over in order to save to CoreData

I have created a tableviewcontroller, with a dynamic prototype cell. Within this view the user has an option to type in a review of a location from a button press as well as can rate the location on a scale of 1 to 10 with a UI Slider. The review is done with a UIAlertController within the tableviewcontroller - but the UISlider is within the cell itself. I am trying to save both pieces of data to core data within the tableviewcontroller. But the UISlider rating value is not available within the tableviewcontroller - is there a way to reference it in tableview from the cell or do I need to have two separate save functions? Here is some of my code thus far - within the tableview controller it doesn't recognize the variable assigned to the UISLider value in the prototype cell. Thanks in advance for any help!
In my tableviewcell:
class WineryInfoTableViewCell: UITableViewCell {
#IBOutlet weak var ratingLabel: UILabel!
#IBOutlet weak var sliderbutton: UISlider!
#IBAction func sliderValueChanged(sender: UISlider) {
var ratingValue = Float(sender.value)
var roundedRatingValue = roundf(ratingValue/0.5)*0.5
ratingLabel.text = "\(roundedRatingValue)"
}
in my tableviewcontroller
#IBAction func save() {
if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {
myRatingData = NSEntityDescription.insertNewObjectForEntityForName("WineryReview", inManagedObjectContext: managedObjectContext) as wineryReview
myRatingData.wineryName = wineryNames
//myRatingData.rating = ratingLabel.text how do I call this for saving?
myRatingData.review = myRatingEntry
var e:NSError?
if managedObjectContext.save(&e) != true {
println("insert error: \(e!.localizedDescription)")
return
}
}
// If all fields are correctly filled in, extract the field value
println("Winery: " + myRatingData.wineryName)
println("Review: " + myRatingData.review)
//println("Rating: " + ratingLabel.text!) how do I call this for saving?
}
I'm still new at Swift, but I think I may have an answer to a part of your dilemma. To reference the UISlider in your table cell, you can use 'tags' to get a reference to it.
For example:
let cell = tableView.dequeueReusableCellWithIdentifier(TableViewCellIdentifiers.someCell, forIndexPath: indexPath) as UITableViewCell
let slider = cell.viewWithTag(100) as UISlider
In the above example, I would have used assigned the UISlider with a tag of 100, so I am able to get a reference to it using the cell.viewWithTag(100).
Forgive me if this doesn't work!
You could pass a WineryReview object into the cell and set its rating directly from sliderValueChanged.
In cellForRowAtIndex:
cell.wineryReview = myReviewData
In TableViewCell:
#IBOutlet weak var ratingLabel: UILabel!
#IBOutlet weak var sliderbutton: UISlider!
var wineryReview: WineryReview?
#IBAction func sliderValueChanged(sender: UISlider) {
var ratingValue = Float(sender.value)
var roundedRatingValue = roundf(ratingValue/0.5)*0.5
wineryReview.rating = roundedRatingValue
ratingLabel.text = "\(roundedRatingValue)"
}
You could call save on the context from the TableViewController if you like. I did something very similar to this in a project.

Resources