iOS - Custom Table Cell Not Full Width of Uitableview

iOS - Custom table cell not full width of UITableView

Can you post the code please .

Did you enable the Use Auto layout and Use Size Classes as below do that first in both table view and table view cell .and tell me your problem

Sample Image

1)After that select both image and label and do as below

Sample Image

2) Select the image and do below

Sample Image

3) select the label an do the following

Sample Image

Can you check the link below:

Table cell content(title) moving left after selecting cell

Tableview cell border not occupying full width in swift

You may find it much easier to use a UIView subclass to handle the "edges."

Add this class to your project:

class BorderedView: UIView {

var edges: UIRectEdge = []
var color: UIColor = .clear
var thickness: CGFloat = 0

var shapeLayer: CAShapeLayer!

override class var layerClass: AnyClass {
return CAShapeLayer.self
}

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
shapeLayer = self.layer as? CAShapeLayer
shapeLayer.fillColor = UIColor.clear.cgColor
}

override func layoutSubviews() {
super.layoutSubviews()

let pth = UIBezierPath()

if edges.contains(.top) || edges.contains(.all) {
pth.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
pth.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
}

if edges.contains(.bottom) || edges.contains(.all) {
pth.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
pth.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
}

if edges.contains(.left) || edges.contains(.all) {
pth.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
pth.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
}

if edges.contains(.right) || edges.contains(.all) {
pth.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
pth.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
}

shapeLayer.lineWidth = thickness
shapeLayer.strokeColor = color.cgColor
shapeLayer.path = pth.cgPath
}
}

Then assign the Custom Class of your containerView to BorderedView. Connect it as normal in your cell class:

@IBOutlet weak var containerView: BorderedView!

Change your cell class to:

class MyTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var containerView: BorderedView!

func configureContainer(toEdges edges: UIRectEdge, color: UIColor, thickness: CGFloat) -> Void {
containerView.edges = edges
containerView.color = color
containerView.thickness = thickness
}

}

Then, in cellForRowAt, instead of calling:

cell.containerView.addBorder(toEdges: [.left, .top, .right], color: .gray, thickness: 1.0)

call it like this:

cell.configureContainer(toEdges: [.left, .top, .right], color: .gray, thickness: 1.0)

Result - with the tableView inset 8-pts on each side so you can see the left/right edges:

Sample Image

When the tableView / cells change size (such as on device rotation), the edges automatically update:

Sample Image

How to increase the width of custom cells in UITableView

The problem is in this code is frame width, somehow the width of the self is not the width of a device, so because of this, you are facing this issue.

lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width, height: 76))
view.backgroundColor = .red
view.layer.applySketchShadow()
return view
}()

To resolve this issue you can set frame like this

let view = UIView(frame: CGRect(x: 10, y: 6, width: UIScreen.main.bounds.size.width - 10, height: 76))

Custom Cell content doesn't take up the entire width of the Table View

After several hours of searching for the solution I decided to start over. I deleted the .xib file, made a new cell and now it's working great.

screenshot

ImageView in tableview cell not taking whole width from leading

Try to move cell to separate nib and then register that nib in viewDidLoad and in cellForRowAt call that cell
Put one view like mainView in contentView and then you can put constraints to that mainView and play it with that.
You should avoid putting cells inside tableView direct it is messy to look at it :D

iOS Swift, cell in UITableView not taking whole width

As the whitespace only appears to the right and the left of your cels, it's clearly part of the cels themselves. Check the Background color of the cell view - it's probably set to white.



Related Topics



Leave a reply



Submit