I have some UITableViewCell with some subview. I need this subview to be shown on the top of the cell is selected and not. The problem is that this cell has selectedBackgroundView which hides my needed subview on select.
UIView *bgView = [[UIView alloc] initWithFrame:cell.bounds];
bgView.backgroundColor = someColor;
cell.selectedBackgroundView = bgView;
I tried also cell.selectedBackgroundView addSubview: but it didn't help.
So the question is: How to add some subview to the cell to make it be on the top if cell is selected?
set cell.selectionStyle = UITableViewCellSelectionStyleNone;
if you have a custom Cell, you can implement
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self bringSubviewToFront:myCustomView];
}
if not, the logic is the same, just in the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method ;)
You must add your subviews to the contentView of the tableViewCell and not the cell directly.
Even if it is a custom subclass of UITableViewCell that you have implemented, you must add other subViews to the contentView of the tableViewCell. The contentView and it's subviews will not be hidden when the cell is selected.
[cell.contentView addSubview:yourView];
Related
I'm adding a subview to my cell like what this answer says.
But when I add it on cellForRowAtIndexPath:, the cell height is updated accordingly (as I modify the height constraint), but the subview only appears after I make a second reload data.
How can I make it update the cell just after I add the subview?
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PastPartyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:HistoryPastPartyCellIdentifier forIndexPath:indexPath];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.mosaicView.frame.size.width, cell.mosaicView.frame.size.height)];
testView.backgroundColor = [UIColor redColor];
[cell.mosaicView addSubview:testView];
cell.cellHeightConstraint.constant = HistoryPastPartiesCellHeight + MosaicHeight;
cell.mosaicHeightConstraint.constant = MosaicHeight;
}
I want to only color my left half or right half of my tableview cell background. How can I manage that? For now, I am only able to set the background color for the entire row. I have tried to put a label on the left half of the cell, and set the background color of the label. But the problem is then the label covered the cell.text so that I cannot see the content of the cell. I really want to know if I can set the label just under the cell text but above the cell's background. so it can does its work. But other approach to accomplish just coloring half of the tableview cell is appreciated as well!
I have tried #winnder.ktw to set it up programmatically. however I got an error image how can i fix that??
thank you!
If you're using interface builder, just change the order of the views in the Document Outline panel at the left, and you can choose what's on top of what:
If all you need it the background there's no need to add a label, just add a regular view to your cell.
Edit: here, just add a view and a label, and make sure the label is below the view in the Document Outline panel.
try this
- (UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier: #"any-cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: #"any-cell"];
UIView* bgView = [[UIView alloc] initWithFrame: cell.bounds];
bgView.backgroundColor = [UIColor whiteColor];
UIView* halfView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, cell.bounds.size.width / 2.0, cell.bounds.size.height)];
halfView.backgroundColor = [UIColor greenColor];
[bgView addSubview: halfView];
cell.backgroundView = bgView;
}
return cell;
}
Following the advice in this answer Custom UITableViewCell selection style? about how to make a custom selection style for a UITableViewCell I did the following.
First, in my custom view controller, which has a UITableView as a property and which serves as its datasource and delegate I have the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell;
NavTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Then this is my entire custom UITableViewCell:
#import "NavTableViewCell.h"
#implementation NavTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if(highlighted){
self.contentView.alpha = .5;
}else{
self.contentView.alpha = 1;
}
}
#end
I have also tried putting the code in setSelected. In both cases I don't see any change in my selected/highlighted table view cell. I have tried changing its color and changing the colors of the labels within it, but there is simply no change. I have also tried going through all the kinds of selection styles for the cell, all to no avail as far as seeing my customization.
What am I doing wrong?
From the documentation:
You can use selectedBackgroundView to give the cell a custom appearance when it is
selected. When the cell is selected, this view is layered above the
backgroundView and behind the contentView.
So whatever you want to do to your cell while selecting it, you have to do it for the selectedBackgroundView .
Try this:
UIView *highlightView = [[UIView alloc] init];
highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
cell.selectedBackgroundView = highlightView;
My Tableview cells expand when tapped, to show more information in 3 UITextViews.
But the cell changes color (as i want it to), and this hides the UITextViews, but not the other subviews as you can see in the image below.(orange lines indicate the textviews).
Changing the color of the textview background makes no difference.
What could i try?
EDIT ------ new image with TextFields.
This issues caused by UITableViewCell selection. When you select cell it changes backgroundColor on all subviews. To prevent this you have two options:
1) Subclass UITableViewCell and set color to subviews in:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
2) Setup color in tableView:didSelectRowAtIndexPath::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isSelected]) {
UIView *view = [cell viewWithTag:TEXTFIELD_TAG];
view.backgroundColor = [UIColor greenColor];
}
}
If i have not misunderstood the question then You can try adding all these three text view in one uiview(container) . Then on didselect table view cell change the background color of that container view. Hope it helps.
Have your cell selection style to none and override setSelected method of tableView cell to change background color of cell.
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
#implementation CustomTableViewCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
//--
self.contentView.backgroundColor = (selected) ? [UIColor grayColor]:[UIColor whiteColor];
}
#end
I have a class PostListTableCell, inheriting UITableViewCell, which is used as my custom cell in a UITableView.
I need to resize some labels in the cell, so I called following method in
- (void)layoutSubviews {
[super layoutSubviews];
[_titleLabel sizeToFit];
}
But the problem is, when the UITableView loaded, the _titleLabel does not resize:
But after I click this cell and select it, the title did resize:
Following is the code to load data:
- (PostListTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PostListTableCellIdentifier = #"PostListTableCell";
PostListTableCell *cell = (PostListTableCell*) [tableView dequeueReusableCellWithIdentifier:PostListTableCellIdentifier];
if (cell == nil) {
cell = [[PostListTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PostListTableCellIdentifier];
}
Post *post = (Post*)_posts[indexPath.row];
[cell loadPost:post];
return cell;
}
Could anyone help?
The problem is the autolayout that resize again the label for you with the constraints that you had set when you make the label in Interface Build, unfortunately in under autolayout the the hierarchy is Constraints > Frame
First of all, if you use Autolayout in (viewDidLoad) method which is related to UIViewController and its subclasses and (awakeFromNib) which is related to UIView and UITableViewCell etc all Views Frames' have not been rendered yet while those methods called.
So if you want to resize any of outlets you should call it in (ViewDidAppear) for UIViewController. In UIViews and cells you should use:
- (void)layoutSubViews
{
[super layoutSubviews];
}
And don't forget to set interval to your Method of resizing as in this answer.
set frame for your label
-(void)layoutSubviews
{
CGRect frame = CGRectMake(20, 30, 200, 50);
_titleLabel.frame = frame;
}