Change textLabel and imageVIew order in a cell in Swift - swift

I'm putting an image and a text label in the same table cell. I want to make the text label followed by the image but the image comes first. Here is the code.
cell.textLabel?.text = "Text label comes here"
let image = UIImage(named: ChangeYTDArrow!)
var imageView = UIImageView(image: image!)
cell.imageView!.image = image
And here is what happens withe the code.
Thank you in advance.

You should probably use a custom tableViewCell in the interface builder, it allows you to everything you want in a tableViewCell like multiple UILabel or everything you want.
To do so, go in your storyboard, select your tableView then on the tableView property set the field Prototype Cells to 1, a new cell will appears and you will be able to design what you want.
Add an identifier to this cell then create a new class for your cell and add your #IBOutlet, here you should have multiple label & an UIImageView()
Then in your controller with textView method, at the cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let reuseIdentifier = "yourCellIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? yourTableViewCell ?? GolfCourseTableViewCell(style: .Default, reuseIdentifier:yourCellIdentifier)
cell.yourLabel1.text = "blahblah"
cell.yourLabel2.text = "blablahbl"
let image = UIImage(named: ChangeYTDArrow!)
var imageView = UIImageView(image: image!)
cell.imageView!.image = image
return cell
}

Related

How to validate UITableviewCell label and Imageview data with UI Test Cases

I have a UITableView with some data coming from a web service. It has multiple UILabels and UIImageView in every cell. How can I get labels and UIImageView from a cell and validate if they have valid data? I want to test it for first cell only
I am able to get the first cell instance as following but unable to get its child elements.
My Code to get first cell:
let app = XCUIApplication()
let myTable = app.tables.matching(identifier: "property_list_tableview")
let cell = myTable.cells.element(matching: .cell, identifier: "cell_0")
Now I want to extract labels and imageview from the cell. I have already added accessibility identifiers for labels and imageviews.
Use following to compare text for label value
let app = XCUIApplication()
let myTable = app.tables["property_list_tableview"]
XCTAssertEqual(myTable.cells["cell_0"].staticTexts["field_name"].label, "Text to compare")
You have an option to add accessibility labels as UI identifiers, and then you can easily select it and get its value.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
let = tableView.dequeueReusableCell(withIdentifier: "CustomCell")! as! CustomCell
cell.yourUIImage.image?.accessibilityLabel = "image " + String(indexPath.row)
}
then in UITests.swift
func testAreImagesLoaded() {
let image = "image "
XCTAssert(XCUIApplication().images[image + String(0)].exists)
XCTAssert(XCUIApplication().images[image + String(1)].exists)
XCTAssert(XCUIApplication().images[image + String(2)].exists)
}
you can access value for string example
let labelElement = XCUIApplication().staticTexts["myLabel"]
...
XCTAssertEqual(labelElement.value as! String, "your text")
but don't forget to add accessibility label for UILabel
see this link for more info https://medium.com/flawless-app-stories/automated-ui-testing-in-swift-ios-46e1c9993316

How to resize the imageView?

How do I resize the imageView?
Xcode says initialisation of imageView was never used.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let city = nicosiaPlaces [indexPath.row]
cell?.textLabel?.text = city
let imageView = CGSize(width: 100, height: 100)
cell?.imageView?.image = UIImage(named: city)
return cell!
}
You just create a new CGSize every time when the method is called, you should change the size in your cell class instead. And if you're using storyboard with AutoLayout, you should change the size in storyboard.
I guess your question has some flaws inside.
Assuming that you need to resize an Imageview I'm posting small Snippet for you.
Never initialize any new component inside func tableView(cellForRowAt:IndexPath)
beacuse every time it is going to create a new Instance until they are properly disposed.
You have taken reference as let imageView = CGSize(--,--) but you are again calling the instance from Cell Method.
Don't do that.
override func awakeFromNib(){
let imageView = UIImageView(CGRectMake(0,0,150,150))
imageView.contentMode = .scaleAspectFill
contentView.addSubView(imageView)
}
Now In the Class method get reference of the ImageView and resize if you want.
overrride func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.deququeResuableCell(withIdentifier: "cellID",forIndexPath: indexPath) as! yourCustomCellClassName
cell.imageView?.image = UIImage(named: "yourImage")
// If you need to resize the Image make changes in Frame of Image or ContentMode of the Image.
return cell
}

change the UICollectionViewcell content from imageTap Gesture Recognition selector method with Swift

I have one CollectionViewcell and i set tapGestureRecognizer action to my thumbnail image on the bottom page ( the small image ). I want the method to change my big image with the small one, but how to access the content of my cell ? I have tried this, i got the imageURL and i got the indexPath but i failed to change the content of my cell
Sample Screenshoot
func tapThumbnail(sender: UITapGestureRecognizer){
let thumbnail = sender.view as! UIImageView
let url = thumbnail.sd_imageURL() as! NSURL
let point = sender.locationInView(self.collectionView)
let indexPath = self.collectionView!.indexPathForItemAtPoint(point)
let cell = self.collectionView!.dequeueReusableCellWithReuseIdentifier(productsIdentifier, forIndexPath: indexPath!) as! ProductsSpotlightViewCell
cell.productImage.sd_setImageWithURL(url)
}
any 1 have simple solution ?
i figured it out!
change my cell declaration to this
let cell = collectionView.cellForItemAtIndexPath(indexPath)

Set action for subview inside a table cell (swift)

When i clicked the subview,it didn't trigger subview's action. But i have selected whole cell. How to fix it?
Here is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("idcell", forIndexPath: indexPath) as UITableViewCell
let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel
lblTitle.text = (deptId[indexPath.row] as? String)! + " " + (deptDesc[indexPath.row] as? String)!
var height:CGFloat = 40
if(indexPath.row == departmentSelectedRow){
for i in 0...deptProfile.count-1{
let label = UILabel(frame: CGRectMake(0,height,400,30))
label.targetForAction("sadasdd", withSender: nil)
height = height+40
label.text = ("ewrewrewre")
label.backgroundColor = UIColor.redColor()
cell.addSubview(label)
}
}
return cell
}
If you want to interact with something inside a cell (rather than select the whole cell) then you should use a button or a gesture recogniser. These are properly interactive elements that you can add a target and action to.
You should also change how you're using cells, specifically:
Use custom cell classes, so you can directly reference the cell elements instead of using viewWithTag which is bad practice
Use multiple different cell classes (and thus identifiers) so you can use actually different cells for selected and unselected rows
Don't create and add subviews on the fly, this is what the different cell classes are for

tableViewCell image property reset when selected - swift

I do create a custom cell with 2 UIImageView inside and a UILabel this way:
let ChildCellID = "ChildCell"
if indexPath.section < 2 {
var cell = SectionCell.loadOrDequeueWithTableView(tableView, reuseIdentifier: SectionCellID)
if cell == nil {
cell = SectionCell.viewFromNib()
}
cell.delegate = self as SectionCellDelegate
cell.avatar?.loadAvatarURL(child.avatarUrl)
cell.avatar.layer.cornerRadius = cell.avatar.frame.size.width / 2
The attribut avatar is the UIImageView I decided to round. When a cell is selected my code goes:
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
let tableViewCell = tableView.cellForRowAtIndexPath(indexPath)
if let cell : SectionCell = tableViewCell as? SectionCell {
cell.selection = !cell.selection
} else if let cell : ChildCell = tableViewCell as? ChildCell {
cell.selection = !cell.selection
}
return indexPath
}
Selection is the checkbox as UIImageView. My problem is when I select a cell, the cell.avatar loses his property meaning it goes back as a square form. The cell.avatar is still loaded and I tried cell.avatar.layer.cornerRadius = cell.avatar.frame.size.width / 2in the willSelectRowAtIndexPath and didSelectRowAtIndexPath but without success.
Am I missing anything that makes my avatar loses his rounded property ?
The only thing that could possibly be the origin of this problem would be the fact that I do use the checkbox UIImageView which is really near my avatar.
I found out that the checkbox near my avatar lacked a constraints that, for some random reason and even though it did not impact the avatar view, was resetting the avatar view rounded property.