I want to have a button in my custom cell which is right aligned. Doing it with auto layout and setting a horizontal space from the right and centering it vertical does not show the Button in my cell because it is too far at the right.
In the picture you can see my cell. But it is not shown like this in the actual app.
Edit: I tried your answer but this does not seem to work. My TableView looks kind of strange in the Emulator:
Is it possible that there is a problem in my controller?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as EventTableViewCell
let event = items[indexPath.row]
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
return cell
}
Do the following for that Button:
Set Width and Height constraints
set Vertical center constraints to the cell view
Set horizontal/trailing(may be 10px) space to the right side of the cell view
Sry I am dumb.
Everything was fine with my Cell layout. I was so focused on the cell that I forgot to add constraints to the TableView. Adding these constraints fixed the problem.
Related
I custom my table view cell in storyboard to make a label center in cell.
but it seems having no affection.
my storyboard is like this:
actually it like this:
I only set label's text in code like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Movie", for: indexPath) as! ArchiveTableViewCell
cell.showChineseTitle.text = chineseTitleArray[indexPath.row]
return cell
}
why?
Thanks in advance!!!
remove leading and top constraint and set the width and height constraint to center the label in the cell. Set textAlignment property of label to center.
Try to add:
cell.showChineseTitle.textAlignment = .center
Becouse you do not set text alignment properly for the UILable. But only set position of it in cell.
My project currently has a view, which is a UIViewController, in that view controller I have a UIViewTable that shows a list of devices that I can tap on to open a settings menu for each device. The list shows up correctly and the tapping actions are also working fine.
My problem is that the display cuts off the cells a little to the left and right. The separators also had a margin to the left but I could fix it in my storyboard by changing the separator inset to custom and = 0.
The overwhelming response to that problem is to add :
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
Under
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
But this had no effect for me. Here's the picture of my display, any help would be really appreciated!
Note: If that helps, when I tap the cell, the highlight does cover the whole width which seems a little odd to me, but I'm not sure what to do with that information.
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.
I have a subclass of UITableViewCell and a subclass of UITableViewController. I'm popualating the tableview with my custom cells.
I created a UITableViewCell in my storyboard, made its class my custom UITableViewCell Swift file, and gave it an identifier. Then I create cells in the controller with:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath indexPath)
return cell
}
In my custom cell (on the storyboard) I have a UIImageView with the following constraints:
Image View Constraints
The constant for the top space constraint is 0, which I thought means the ImageView top would be flush with the cell top.
However, the images in my cell are pushed down a bit:
UITableView with custom cell
The white is just the table section header, that's working perfectly. However, you can see the orange gap that appears above the image (that orange is the background color of the cell), and I don't want that there.
I tried setting the image frame in the custom cell's awakeFromNib:
#IBOutlet weak var img: UIImageView!
override func awakeFromNib() {
img.frame = CGRectMake(0, 0, img.frame.width, img.frame.height)
}
But this had no effect, so I tried setting the frame in layoutSubviews:
override func layoutSubviews() {
img.frame = CGRectMake(0, 0, img.frame.width, img.frame.height)
}
This worked, but only after scrolling down and back up on the TableView. The first two cells have the orange gap, but if you scroll to the third one and back up, then the gap is gone.
Does anyone know why this is happening? How can I make the imageview be positioned at (0, 0) right away instead of only after scrolling down and back up?
When I try to reproduce your situation and drag the imageView to align to the top of the cell the Xcode suggested constraint is -8, and it works fine like that but if i set it to 0 I get the background color like your problem, try setting the top constraint to -8 or whatever Xcode is suggesting.
I'm not very familiar with AutoLayout so I can't clarify why Xcode wants to set it to -8
Try to set the height of tableViewCell through the following code:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UIScreen.mainScreen().bounds.width
}
I have a dynamic Prototype TableView. As I want to show a line in the cell so I drag a uiview and placed in the cell. The problem is when I compile the app in mobile the line doesn't show up in the cell. I am doing this in my code to fill the cell values
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell",
forIndexPath: indexPath) as! RequestTableViewCell
cell.userNameLabel.text = firstName.uppercaseString + " "
+ lastName.uppercaseString
cell.contentView.addSubview(cell.tableSeparatorUIView)
}
I think I am doing something wrong here
cell.contentView.addSubview(cell.tableSeparatorUIView)
I have done the sample example. Use auto layout. Go throght sample example Please go through the link. it will helps you.
Do this :-
Select your TableView do separator None in attribute inspector. Then add UIView at bottom with height 1px and leading, trailing,bottom to tableView. .
I have no idea why, but when I touch a cell, image inside it kind of zooms and is partially of screen. Below you can see, the first row looks like it should and the second row was selected and now looks zoomed.
There is not much code, only ViewController and TableViewCell that both have minimum required code.
EDIT: Here's how I add cells, and there is no didSelectAtRow.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCell
cell.myImage.image = UIImage(named: "uk.png")
return cell
}
EDIT2: Initially my imageView constrains where 0 to all margins, so that whole cell would be only image. When I made image quite small, none of zooming happens, so maybe I am doing something wrong with constrains?
Final Edit - Problem Solved
Not sure, how that zooming happened, but in my case trying different constrains solved the problem.
It is because your tableview is reuse its cell
First set the constraints of the cell.
and
also implement the
the code in cellForRowAtIndexPath with
if(cell == nil)
{
cell = [[UITableViewCell alloc] init];
}
also share your code for didSelectRowAtIndexPath