Table cell's content view is being shifted to the left if an accessory is added in IB - ios

I've got a custom table view cell defined within a nib, I'm finding that whatever colour I set for view background in IB isn't being applied (why its not being applied is another question I have). So within the table view controller I added:
[localCell.contentView setBackgroundColor:LIGHT_BACKGROUND];
The cell colour now comes out as expected, however if in IB I set the accessory to anything other than none then the colour of the cell is shifted to the left by an amount about equal to the size of the indicator, behind the indicator I can see the background colour for the table.
Why is this happening?
TIA

The accessory view is not contained within the content view. You probably need to set the color of the background view of your cell rather than the content view.
[localCell.backgroundView setBackgroundColor:LIGHT_BACKGROUND];

Related

Button not responding in Table View Cell [duplicate]

This question already has answers here:
Programmatically send to front/back elements created from interface builder
(2 answers)
Closed 10 days ago.
I am using a .xib for my cell in a table view. Inside the .xib, is another view (cellBackgroundView), and a button. When I run the app, and click the button, it does not respond at all. Instead, it calls the tableView's didSelectedRowAtIndexPath method which brings another view controller.
Using Xcode's Debug View Hierarchy, I discovered that I have a view overlaying the all the buttons (see pic attached: this overlaying view is highlighted). This view (called backgroundView) that is overlaying my button is a View, within a view. I have a feeling when you place a view in a view, and put a button in the initial view, the button isn't called because its below the view hierarchy.
How do I fix this issue? Is there a way to move background view to the back of the view heirarchy so that the buttons will be responsive?
Debug View hierarchy:
Structure of .xib
Two things that you could check
1) Do you have a delegate method for cell height and is the height returned correct? Unless you have Clip Subviews on for the UITableViewCell, the contents of the cell can be visible outside it's frame, but the parts that are outside the cell's frame are not registering user interactions.
2) Is some other view element higher in the hierarchy (lower in the XCode listing you posted) overlapping the button? iOS Simulators Debug -> Color Blended Layers can help spotting this.
Edit:
If I interpret the added screenshots correctly, you probably have the issue mentioned in the option 1) above. If the other elements showing in the screenshot are those listed as subviews of the Cell Background View they are mostly outside the parent view's frame and thus don't receive touch events. If the background view's frame is correct, then you might want to move the other elements as children for Feed Cell directly.
Also, the element listing suggests that you are using plain UIView as the parent element. I don't know the inner workings of your application, but if you only use this view in a UITableViewCell you might want to consider making the parent view a Table View Cell in the xib. This will reduce some bloat and allow you to configure some properties for the cell in the xib.
Maybe you forgot assign your Button to code
I assume Feed Cell is a subclass of UITableViewCell, and cellBackgroundView is the property contentView of this cell.
If so, the cells property backgroundView should be behind your cellBackgroundView (the docs say: UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.).
You could set the cells property backgroundView = nil, and see whether it is still there in the view hierarchy. If so, you do add a custom backgroundView on top of the other cells views somewhere.
To check this, you could read out the subview hierarchy of your cell in your method tableView:cellForRowAtIndexPath: with something like NSArray *svs = cell.subviews; (assuming cell is the tableViewCell) and set a breakpoint behind this statement.
svs usually contains first the UITableViewCellContentView, and above it the _UITableViewCellSeparatorView. The cells backGroundView will not be shown. You could check there your view hierarchy.
If by chance there is a custom backgroundView on top, you could - as a workaround, not a solution - bring the contentView to the front by sending to the cell bringSubviewToFront: with the contentView as argument. Then the button should respond.
In your Structure of Xib Place your button below the view that is first the view is added to superView then the Button, then your button will work.
or you can code
-(void)viewDidLayoutSubviews{
[self.view insertSubview:yourButton aboveSubview:cellBackgroundView];
}
Hope it will help.
Do you add an UIButton by code?
If so you should ensure you addSubview: into cell.contentView and not into cell.
Also you could try to apply CellBackground class to a view inside contentView, not directly to contentView.
you can use this method.
[cell.contentView bringSubviewToFront:yourButton];
after that if you want to back in background then tou can use sendSubviewToBack: method .
After adding this methods your button is not responding set the userIntractionEable of button's superview.

ios tableView clearColor seperator

I have set my tableView's background color, and I want there is space(10px) around each cell show that color.
e.g.(ignore the red line)
What I am doing now is make the cell embed a smaller view, and set the cell's background to be clear. But it is annoying to create such embed view again and again.
So, I am seeking for a better solution, I believe it would be good if i can increase separator's height. But the old answers did not solve my problem.
(How to increase the UITableView separator height?)
EDIT:
However, if I add my own separator in cell. I may not able to set the cell having round corner
Set the tableView's separatorStyle to .None.
Create a custom UITableViewCell subclass.
In the storyboard (or xib if you created one for the custom cell), add a subview that's inset the amount that you want padded on all four sides.
If you want rounded views, do that on the custom cell's subview (e.g. subview.layer.cornerRadius = 10.0, subview.layer.masksToBounds = true).
Set the custom cell's ContentView to have a clear background.
Set the table view's background to that lovely shade of blue.
Set the cell subview's background color to white.
You don't have to use the cell repeatedly in InterfaceBuilder. Just give the cell an ID, and in your implementation of the table view's data source, dequeue a cell instance using that ID and set the values of the labels inside it.
Have you thought of just making the cell height taller and putting your own separator on the bottom of your custom cells? Nice table layout by the way.

How do I structure a table view to load halfway down a view?

I would like to make my UITableView load covering half of the window with a background view that starts visible, and as the window scrolls, is gradually covered by the table's cells.
Initially, I want to style is like so:
As the user scrolls, the cells will cover the background element.
The only way I could think to accomplish this would be to put the background element behind (and outside) a ScrollView (which is sized to fit over the background element and has a transparent background), put the TableView inside the ScrollView, and set the load coordinates to start the table far enough down. Is there a better way to accomplish my desired design?
I would prefer to avoid this layout because I would like to be able to easily add more cells dynamically as the user scrolls, ideally without having to deal with resizing the ScrollView.
You can take a view inside table view just above the table view cell and make its height whatever you want space in table view and make a outlet of that view. Then in view did load assign the view as table header view like this.
self.tableView.tableHeaderView = self.headerView;
This will be displayed exactly like you given and when user scrolls it will show the below cells and when you want you can change the frame of the header view and your header become invisible. You can do it whenever you want i mean if you want to change height on scrolling or if you want to change on adding more cells.

UITableView: scrolling outside bounds hides cells

I have an UITableView that does not fill the whole screen, so there is some space at the top and the bottom of the screen. This table uses table.clipToBounds = NO and table.bounces = YES.
But when scrolling the cells outside the original frame of the table, the cells are hidden. I know that's the normal behavior of UITableView to increase performance. But is it possible to define an area at the top/bottom of UITableView within which the cells are not hidden? Or even set a cell property to be "always" visible?
Subviews outside bounds of superview are hidden (table view's cells are also its subviews). That's just how iOS view hierarchy works, not really about performance. And it doesn't make any sense when a subview/cell is displayed on the screen while user cannot interact with it (because it's out of bounds). In particular, table view reuses cells that go out of its bounds, so no, you cannot set a cell to be "always visible".

Kind of complicated custom UITableViewCell

I have a particular goal in mind here, searching for it is a little hard. I am trying to accomplish this (This is a photoshopped screenshot):
I have everything in this view working, except for the split row for the Company Name/ Beginning of the field row. The "Company Name" field is just a textfield, all I really want to do is shrink that neato cell background to just go behind the right side.
Create a custom table view cell that has two subviews: the text field on the left and a UITableViewCell on the right as a subview of the main table view cell. A UITableViewCell is just a UIView so you can actually add it as a subview of any view. The main table view cell will have it's background color set to transparent.
So totally complicated custom cell comes with a totally ridiculous solution. I built a view for the cell that has the one field...and another UITableView.
That second UITableView has the "Beginning of the field" text, and its cell gets the background, and I hide the background of the main cell.
I had to play around with the nested table's size and position to get the row to display properly, and make sure that the lines in the background don't shift when it hits the nested table, but it came out perfect
You could try setting the frame property of your cells backgroundView to cover only have your cell's width. Address Book handles complex forms like this with a nice look and feel--you might want to see what they've done there..
One approach would be to define a custom table view cell, set its background transparent and add the UITextField on the left and a UIButton on the right (customize it to look like your other cells), as in your screenshot.

Resources