How to redesign horizontal cells to vertical cells - ios

This is what all of my cells look like. With some information displayed on right hand side of the cell. Instead of this design with a circular radius image on the left hand side and information on the right hand.I want my cells to be an Image and the information to be on top of that image.
This is the type of style I want on my cells to have, without the space in between images, I gave this an attempt by removing the information labels and using a horizontal Stack View on the cell.
But this is what the cells ended up looking like. For the radius of the image I didn't do it programatically I did trough the Identity Inspector
Keypath layer.cornerRadius
Type Number
Value 30
which makes the images round. Other than that there's no much code involved in the tableView, It's all mostly done trough the Main.StoryBoard, below I added some code showing how I configured the cells. Any tips or ideas on how I can accomplish this?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! RestaurantTableViewCell
//* Configure the cell...
cell.nameLabel.text = restaurants[indexPath.row].name
cell.thumbnailImageView.image = UIImage(named: restaurants[indexPath.row].image)
cell.locationLabel.text = restaurants[indexPath.row].location
cell.typeLabel.text = restaurants[indexPath.row].type
cell.accessoryType = restaurants[indexPath.row].isVisited ? .Checkmark : .None
return cell
}

If you want the cell to show an image and then show text on top of that image, I'd suggest you set the image as the cell background view (rounding the corners if needed) and then populate your labels and accessory view as you are already doing.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let dictData : NSDictionary = arrayOfData.objectAtIndex(indexPath.row) as! NSDictionary
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = "THIS IS SOME TEXT"
if let image = dictData.objectForKey("Image"){
cell.backgroundColor = UIColor.clearColor()
let imageView = UIImageView(image: image as? UIImage)
imageView.layer.cornerRadius = 30.0
imageView.clipsToBounds = true
cell.backgroundView = imageView
}
return cell
}
That will give you text on top of an image :

So it looks like you use AutoLayout problem here is that you have right constraint. You can try removing constraint and set fixed width.

Step 1: Add UITableView and add constraints
Step 2: Add UIImageView and set it to "Aspect Fill" Otherwise your rounded corner image will not cover whole cell. (You can also set it to "Aspect fit" if you want). And add constraints as shown in image.
Step 3: Add labels and other information and also add constraints according to your requirement.

Related

Can't make ImageView a circle in a Custom Table View Cell

I am trying to display a rounded image for every custom cell in a table view. I am already changing its cornerRadius, but for some reason the image still will appear fully - even when the ImageView's borders are already set round.
Here's a piece of the code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> CustomTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celda", for: indexPath) as! CustomTableViewCell
cell.imgView.layer.borderWidth = 1
cell.imgView.layer.borderColor = UIColor.black.cgColor
cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
//...the rest of function
//image is added to imgView eventually
}
And here is the result in the sim
I tried putting the code in override func awakeFromNib in the cell file but I had the same result.
Any advice is welcome :)
Please try this code by following step:
step1:
Make the height and width same of image view. Add the 1:1 ratio in storyboard in image view for equal height and width.
step2:
cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
cell.imgView.clipsToBounds = true
It may helps you.Thank you.

label having sizeToFits() goes out of bounds

I have a tableview in where each cell has a label. The datasource of this label is from the firebase api. Now, initially the label is loaded in a perfect form. As you scroll through and if any label is of a shorter text width, the rest of the cell, alter their labels to this size.
I even tried applying a stackview around it, but i couldn't help much
Below is the code of cellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as? FeedCell {
cell.caption.text = ""
cell.configureCell(post: post)
cell.caption.sizeToFit()
cell.delegate = self
return cell
} else {
return FeedCell()
}
}
code for function in TableviewCell
func configureCell(post: Posts, img: UIImage? = nil) {
self.posts = post
self.caption.text = posts.caption
}
I am really unable to fix this. Any help is much appreciated.
How about using auto-layout to set a minimum width in percentage of a screen size IOS.
Auto-Layout constraint has that multiplier parameter that lets you use a fractional relationship between a superview and its subview.
While both the child view (Label) and its superview are selected, add "equal width". Then change the "multiplier" of the constraint you just added to the proportion you need. For example, for 30%

How do I remove the selection border on a table view cell?

As you can see, there's a border when selecting. I want to remove this border.
I've tried:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = roomsTableView.dequeueReusableCellWithIdentifier("roomsCell", forIndexPath: indexPath) as! RoomTableViewCell
cell.room = rooms[indexPath.row]
cell.layer.borderWidth = 0.0
cell.contentView.layer.borderWidth = 0.0
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
That doesn't work.
self.roomsTableView.separatorStyle = UITableViewCellSeparatorStyle.None is already set.
Clarification: Look at the middle row. It's selected. You see the border above and below the couple? That's what I want to remove.
I'm going to assume you want to remove the separator from your TableView. You can do this with:
tableView.seperatorStyle = UITableViewSeparatorStyle.None
What is the height of the imageView compared to the cell? If the imageView is smaller by 2 points, the cells background will show through on the top and bottom of the imageView.
Another thing to look at is subclassing UITableViewCell and handling the selection state accordingly.
just for anyone having the same issue setting the
SelectionStyle = UITableViewCellSelectionStyle.None
of your UITableViewCell should do the trick. That will remove the selection border (not the separator) on the item.
Select the tableView and go to the attribute selector in the storyboard file, Then do the followings according to image attached
make Selection to no selection and uncheck the show selection on touch
You can prevent this behaviour by setting
selectionStyle = .none
On your cell awakeFromNib() method.

Only set the value once when two table view cells have the mostly the same components

I have two prototype cells. Most of the UI are the same except one has a image view and the other one don't. The situation is that in cellForRowAtIndexPath, when I set the value for these cell's labels. I have to set them each time in two cells. Can I just set them once and only set the image for the cell only has a image view?
For example:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let place = places[indexPath.row] as Place
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as ImageCell
cell.nameLabel = names[indexPath.row]
cell.screeNameLabel = screenNames[indexPath.row]
cell.createAtLable = place.createdAt.shortTimeAgoSinceNow()
cell.profileImageView.hnk_setImageFromURL(imageURL)
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("BaseCell", forIndexPath: indexPath) as BaseCell
cell.nameLabel = names[indexPath.row]
cell.screeNameLabel = screenNames[indexPath.row]
cell.createAtLable = place.createdAt.shortTimeAgoSinceNow()
return cell
}
}
As you can see, both cells have the same nameLabel, screeNameLabel, createAtLable. How do I only have to set them once? The above code is only a example. My current project, I have a lot of ui in the cells and they are basically the same except one has a image and one don't.
And by the way, what's the best way for this situation. When a cell contains most the same components, only a few components are different. Are using multiple prototype cells the best way?
You can solve it by auto layout. You can use the same cell prototype. What you need to do is set the height of image view to "greater than or equal to 0" if you want to let it expand according to different image height or "less than or equal to some constant if you want the image view height to be fixed. Since imageView has intrinsic content height and width it will expand or shrink according to what you load. The key is to make image view height 0 when there is no content.

add image to uitableview cell

I have a tableview, how can I add image to the left of this cells?
cell.imageView.image = [UIImage imageNamed:#"image.png"];
UPDATE:
Like Steven Fisher said, this should only work for cells with style UITableViewCellStyleDefault which is the default style. For other styles, you'd need to add a UIImageView to the cell's contentView.
Try this code:--
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"arrow2.png"];
[cell addSubview:imv];
[imv release];
Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:
cell.imageView.image = someImage;
If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (e.g. make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:
// DO NOT!
[cell addSubview:imv];
// DO:
[cell.contentView addSubview:imv];
For my fellow Swift users, here is the code you will need:
let imageName = "un-child-rights.jpg"
let image = UIImage(named: imageName)
cell.imageView!.image = image
Swift 4 solution:
cell.imageView?.image = UIImage(named: "yourImageName")
Swift 5 solution
cell.imageView?.image = UIImage.init(named: "yourImageName")
All good answers from others. Here are two ways you can solve this:
Directly from the code where you will have to programmatically control the dimensions of the imageview
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
...
cell.imageView!.image = UIImage(named: "xyz") // if retrieving the image from the assets folder
return cell
}
From the story board, where you can use the attribute inspector & size inspector in the utility pane to adjust positioning, add constraints and specify dimensions
In the storyboard, add an imageView object in to the cell's content view with your desired dimensions and add a tag to the view(imageView) in the attribute inspector. Then do the following in your viewController
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
...
let pictureView = cell.viewWithTag(119) as! UIImageView //let's assume the tag is set to 119
pictureView.image = UIImage(named: "xyz") // if retrieving the image from the assets folder
return cell
}

Resources