Remove a subview from a UITableViewCell's content view - uiview

How do I remove a Subview from a UITableViewCell's content view?
For instance, I have added the following subview to my cell's content view.
UIButton *b = etc.
[cell.contentView addSubview:b];
Now I'd like to remove it:
?

This code remove all subviews on cell:
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}

One line
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];

Related

How to Check If UITableViewCell Contains UIView

I am adding subview to UITableViewCell i.e
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault` reuseIdentifier:cellIdentifier];
Here _cellBackgroundView belongs to UIView.
[cell.contentView addSubview:_cellbackground];
I want to check by the help of isDescendantOfView if it contains _cellbackground, but I am getting warning.
if (![_cellbackground isDescendantOfView:[cell subviews]]) {
[cell.contentView addSubview:_cellbackground];
}
else{
[_cellbackground removeFromSuperview];
}
references Check if a subview is in a view
Please help
[cell subviews] return array , but you need to give UIView as input parameter for isDescendantOfView: method , try like this it will work
if (![_cellbackground isDescendantOfView:cell.contentView]) {
[cell.contentView addSubview:_cellbackground];
}
else {
[_cellbackground removeFromSuperview];
}
[cell subviews] should be replaced by a object of UIView

What subview are all the cell subviews located at? (In the custom cell class)

I have a custom UITableViewCell with a class linked to it.
In awakeFromNib, (of the custom cell class,) I made a for in loop:
for (id view in self.subview)
{
if (view isKindOfClass:[UITextField class]) {
UITextField *textField = (UITextField *)view;
[textField setBackgroundColor:[UIColor redColor]];
}
}
When I run it on the simulator, no textFields background color changes.
I'm pretty sure, that what I have wrong is: self.subview. What should I put in place of that?
Give a tag to your view in the cell that contain textfield let suppose tag = 0
for (UIView *view in [self viewWithTag:0].subviews)
{
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
[textField setBackgroundColor:[UIColor redColor]];
}
}
Hope this help.
Get subviews from self.contentView -
Try the following code
for (id view in self.contentView.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
[textField setBackgroundColor:[UIColor redColor]];
}
}

Add subviews according To UITableViewCell CGRect

I'm trying to add subviews to a view according to the cell of the tableview .. i've done this code already
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VerseCell *cell = (VerseCell *)[tableView cellForRowAtIndexPath:indexPath];
rowSelected = indexPath.row;
CGRect cellRect = cell.frame;
UIView *cellView =[[UIView alloc]initWithFrame:CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70)];
UIImageView* background =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FavoritePanelBackground"]];
background.frame =CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70);
[cellView addSubview:background];
[cellView setBackgroundColor:[UIColor redColor] ];
UIButton * favoritsB = [[UIButton alloc]initWithFrame:CGRectMake(cellRect.origin.x+10, cellRect.origin.y+15, 60, 40)];
[favoritsB setBackgroundImage:[UIImage imageNamed:#"MofadalatNormalIcon.png"] forState:UIControlStateNormal];
[favoritsB setBackgroundImage:[UIImage imageNamed:#"MofadalatTappedIcon.png"] forState:UIControlStateHighlighted];
[favoritsB addTarget:self action:#selector(favorite) forControlEvents:UIControlEventTouchUpInside];
[cellView addSubview:favoritsB];
cellView=nil;
}
the problem is .. that this code work as expected in the first row only.. and when click on next cell it shows the main UIVIew (which i've mark with red background) in the expected position while the sub views of it is going really far down... so what i'm doing wrong here.. and is there another approach for what i'm doing if that will not work?
Where are you trying to add these view? Inside the table view cell? If so, you never add cellView as a subview of cell. IF that's what you're trying to do, add this line just above cellView = nil:
[cell addSubview:cellView];

need assistance regarding looping through subviews

I have added a uibutton in my uitableviewcell and trying to change its background image when it is taped here is the code
-(void)downloadImage:(UIButton *)link
{
UITableViewCell *cell = (UITableViewCell*)[link superview];
UIButton *view = [[UIButton alloc]init];
NSArray *subviews = [cell subviews];
for (view in subviews)
{
if([view isKindOfClass:[UIButton class]])
{
view = (UIButton*)subviews;
[view setBackgroundImage:[UIImage imageNamed:#"yellow"] forState:UIControlStateNormal];
}
}
...
but its not working
if i add this line
view = (UIButton*)subviews;
i am getting this error
Thread 1:signal SIGTRAP
and without this line nothing is happening, any idea what is going wrong?
It depends on how you're adding your subview to your UITableViewCell in your:
-(UITableViewCell)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID = #"cellID";
UITableViewCell *cell = [tableView dequeReusableCellForID:cellID];
if(cell == nil)
{
...
myButton = [[UIButton alloc] initWithFrame....];
[myButton addTarget:self selector:#selector(downloadImage:) forControlEvent:UIControlEventTouchUpInside];
// -------------------------------------------------------
// This is the important part here.
// Usually, we add "myButton" to the cell's contentView
// You will need to match this subview hierarchy
// in your "downloadImage:" method later
// -------------------------------------------------------
[cell.contentView addSubview:myButton];
}
return cell;
}
-(void)downloadImage:(id)sender
{
// -------------------------------------------------------
// Here, "sender" is your original "myButton" being tapped
//
// Then "[sender superview]" would be the parent view of your
// "myButton" subview, in this case the "contentView" of
// your UITableViewCell above.
//
// Finally, "[[sender superview] superview]" would be the
// parent view of the "contentView", i.e. your "cell"
// -------------------------------------------------------
UIButton *button = (UIButton *)sender;
// button now references your "myButton" instance variable in the .h file
[button setBackgroundImage:[UIImage imagenamed:#"filename.png"] forControlState:UIControlStateNormal];
}
Hope that helps.
As for why your app crashes when you do:
view = (UIButton*)subviews;
That is because "subviews" is an NSArray of all the subviews. You're telling iOS to typecast an NSArray * to a UIButton *, which it doesn't know how to do. So you end up causing the app to crash.
In your for loop, you probably want to do something like this instead (although you shouldn't need to if you use my above "downloadImage" method):
for (view in subviews)
{
if([view isKindOfClass:[UIButton class]])
{
// ------------------------------------------------
// When you go for(view in subviews), you're saying
// view = [subviews objectAtIndex:i]
//
// Hence view = (UIButton *)subviews become
// redundant, and not appropriate.
// ------------------------------------------------
//view = (UIButton*)subviews; // <--- comment out/delete this line here
[view setBackgroundImage:[UIImage imageNamed:#"yellow"] forState:UIControlStateNormal];
}
}

UITextView inside UITableViewCell hides cursor when cell is selected

I have UITextView subviews inside a UITableViewCell. When user taps on the cell I select it, i.e. I switch the background image to selected state. The UITextView becomes first responder, but the cursor is hidden. When I disable the cell selection the UITextView shows a cursor without issues.
How to select the cell and start editing the UITextView with a visible cursor? :)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected && ![textView isFirstResponder]) {
[textView setUserInteractionEnabled:YES];
[textView becomeFirstResponder];
} else {
[textView resignFirstResponder];
[textView setUserInteractionEnabled:NO];
}
}
Cell stopped to hide cursor, when i set.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
But after that, cell also stopped use selectedBackgroundView. So I set my image to highlighted state of backgroundView.
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"input_up.png"] highlightedImage:[UIImage imageNamed:#"input_up_act.png"]] autorelease];
And switched image in setSelected:animated:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected && ![textView isFirstResponder]) {
[textView setUserInteractionEnabled:YES];
[textView becomeFirstResponder];
} else {
[textView resignFirstResponder];
[textView setUserInteractionEnabled:NO];
}
[(UIImageView *)self.backgroundView setHighlighted:selected];
}
your cursor non disappeared maybe is't white!
try: [self.tableView setTintColor:[UIColor redColor]];
You could try setting the selected property of UITableViewCell inside the UITextViewDelegate protocol method textViewDidBeginEditing:.
If this does not work, create a UIImageView as a subview of your cell and change the content of that in the method above.
When you select the cell, the textView become the first responder, but the cursor has been hidden, you can deselect the cell, and then the cursor will show.

Resources