Trying to add two images in a UI image - swift

Im new to xcode and was trying to add different images in a table row in a storyboard layout.
let image:UIImage = UIImage(named:
”ster.jpg")!
when i use this code, it makes every table the same image
let image:UIImage = UIImage(named:
”ster.jpg", "blue.jpg")!
My goal is for the 2nd row of the table to have a different image, so when using this code it just creates an error saying " Extra argument in call"

I would start by saying that the way you initialize the UIImage object is wrong. It supports only one image at a time. Thus, you get the error of an extra argument in the call.
To add two images in a row, you better use a horizontal stack view and modify some of its properties (fill equally, spacing, etc).
The way I would do the thing: Drag and drop from library 2 UIImage objects. Embed them in a horizontal stack view. In property inspector set the fill property to equal. Make sure to add constraints.
In case you are using UITableView and want to modify the cell, you will have to create a subclass of UITableViewCell and give it an identifier to use when the TableViewDataSourceDelegate object is fetching the cell data based on the identifier.

Related

Adding a full width row to a CollectionView two-column structure

I created such a structure with CollectionView, there are two items in one line. The whole structure should go on like this, everything is fine until here, but I must add (only 1) slide at the top. So I want to place one more full width image in 1 line. What path should I follow?
An image of my app
If the full-width image that you need to display is just a first item of the data set you're displaying, make the collection view return a custom size for it based on its index path. In order to do it, assign a delegate to your collection view and implement the UICollectionViewDelegateFlowLayout protocol, specifically the collectionView(_:layout:sizeForItemAt:) method. Alternatively, if you're targeting iOS 13+, you can do the same with the UICollectionViewCompositionalLayout, but the implementation will be different, in that case I encourage you to read its documentation.
If the item doesn't really belong to the data set, better create a section header with just that item inside.

UIPickerView and UI Unit Test: How to get values of UIPickerWheel?

Recently I had a bad headache (and I'm still struggling) to find out how to retrieve all values inside an UIPickerWheel. For me should be enough to move at particular row of the wheel, but I can't! So frustrating! I tried to scroll row by row to retrieve all values (https://stackoverflow.com/a/39300344/821407) but it's so slow! Any clue?
NB: I can't use adjustToPickerWheelValue because my root problem is that I don't know the value since they are dynamic and I would like to avoid launchArguments/launchEnvironment.
This is probably not the answer you were hoping for but it is not possible to get the title of all rows in a UIPickerView in a UITest.
As you know when running a UITest you can only access your app's UI elements via the XCUIElement class. That class has a value property that gives you some information about the UI element you access. When accessing a UIPickerView the value gives you the title of the currently selected row. But only of the selected row. You can access the picker's row elements, but unfortunately the value property for the row elements is always empty. So, no luck here. All you the info you can get is the number of rows of your picker.
This is not really surprising though. Even if you had access to the UIPickerView, you could not access the titles of all rows directly. UIPickerView does not know about the titles that it displays. It is the job of the UIPickerViewDataSource to provide the titles.
So, unfortunately, if you need to know all the row titles of your UIPickerView in a UITest, you really have to select each value one by one via your app's user interface.
But it does not have to be as complicated as in the answer you linked. Instead of simulating a scroll you can simply tap on the next row to select it (should be slightly faster):
let pickerView = app.pickerWheels.element
let numRows = pickerView.children(matching: .any).count
var values: [String] = [pickerView.value as! String]
for _ in 0..<numRows {
pickerView.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.55)).tap()
values.append(pickerView.value as! String)
}
In your question you do not really describe what you are trying to test. But if you want to test if the picker has the correct row titles maybe a UnitTest would be a more practical approach?

Swift: Simple - how do I refer a button's name dynamically?

I've been searching this goddam question for over an hour and it's so simple!
I have 9 images and I want to refer to any one of them in a function. They're all named "img1" "img2" etc. They're also linked to my code with IBOutlets.
Func changeImage(imgNumber: Int){
imageName = "img" + String(imgNumber)
self.imageName.image = UIImage( named: "smiley.jpg") }
Instead of the imageName variable I know I can just refer to the actual object by saying "self.img1.image" but that wouldn't allow me to dynamically alter any image with a function.
Please help!
Put your image in an Asset Catalog.
(You can create one by New file... > iOS Resource > Asset Catalog).
In the asset catalog give your images name like image1, image2, image3 etc.
UIImage(named: "image1") will find images in the Asset Catalog.
The benefit of using Asset Catalog is that you can easily add images supporting the different screen resolutions there as well.
Optionally load them manually, and put them in an array and access by index if there is a logical order to be used.
You should put them in to a collection (in viewDidLoad for example) that preserves the order, ie.
var images = [self.img1, self.img2, self.img3]
and use the index to access the specified button
images[idx].image = UIImage(named: "smiley.jpg")
If you need even more dynamic order, you could use tag for this and traverse the subviews to find the specified view. I wouldn't recommend this though, as it is a pain to debug.
You could use outlet collections but to my understanding they don't preserve the order. This would mean you'd have to find another way to sort the buttons (x-coordinate, y-coordinate or tagging).

UITableViewController- showing only rows with content and an image where there is none

When creating a UITableViewController there are two situations that create an "ugly" UX
calling/open it without any data in it --> shows an empty table (i.e. empty rows,UITableViewCell, as many as fit in the window)
calling/open it with fewer rows of content that fit the window --> show the full rows followed by empty rows
I wish to receive the following result:
if there is no data show a picture or view with text - there isn't any data yet or something like that
show only the full lines and no more rows (blank or background image)
Is there a way to achieve that?
To add these effects, you will probably have to make your own UITableViewController from a regular UIViewController, and even subclass UITableView. If you have a regular UIViewController with your filler image/text as the background, you can place a UITableView on top and hook up the delegate/datasource. Now, in your code, detect when there is no data available and set the hidden property of the UITableView accordingly.
As for the following empty rows, you will either have to turn off the row separators (in IB), or subclass a UITableView (can't help you there). Good luck!

do I need separate UILabel in a custom UIView to have separate UILabel lines in a UITableViewCell?

do I need separate UILabel in a custom UIView to have separate UILabel lines in a UITableViewCell?
That is, if I want to have a TableViewCell that has all text, but the text needs to contain 4 separate rows for 4 separate strings (e.g. Name, Title, Description, Location), and each these separate rows could include a wrap around.
To ask the question the other way around, is there way with a normal UITableViewCell using it content view and single text label, to force Carriage Return/New Line points at the end of each of the four strings? Oh yes, and the cell height will need to be calculated for each Cell as it may vary (just in case this is significant)
The answer to the last question: NO. (Answer to the title: YES.)
You can build a cell in its own NIB file. An exercise I suggest if you've never done it.
Layout the size/location/resize functionality as you like it.
your table view controller can be the owner of the file,
add an outlet to the TV controller of loadedCell, and call load nib
every time you want to alloc a new cell,
i suggest tagging each of the cell labels, and accessing them
that way, and setting the loadedCell value to nil after loading it,
p.s. a UILabel often wraps text undesirably, or is hard to layout in a cell to look good, consider the other values of lineBreakMode for your labels
p.p.s. it will employ a text shortening behavior depending on adjustsFontSizeToFitWidth and minimumFontSize (taking this and lineBreakMode into consideration)