UITextView inside UITableViewCell hides cursor when cell is selected - ios

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.

Related

Selected cell hides the cells UITextView subviews

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

custom UITableViewCell reuse highlight issue

in the project I'm on, there is already a custom UITableViewCell and I have determined the issue is in the reuse of the cell.
These 2 methods are doing the overriding of the highlighting and selecting:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (self.isCellEditing == NO) {
if (highlighted) {
self.customView.backgroundColor = [UIColor redColor];
} else {
self.customView.backgroundColor = [UIColor whiteColor];
}
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (self.isCellEditing == NO) {
if (selected) {
self.customView.backgroundColor = [UIColor redColor];
} else {
self.customView.backgroundColor = [UIColor whiteColor];
}
} else {
if (selected) {
self.editImageView.image = self.editAccessorySelectedImage;
} else {
self.editImageView.image = self.editAccessoryImage;
}
}
}
what's happening in the app that is causing the issue is that I need to auto-scroll and select the last selected cell before the app closed on app launch (done in viewDidAppear). This works, except for highlighting the cell, it does in fact scroll to the cell selects it, as it shows in the details view (iPad splitview setup), but the cell will not highlight. This is a reuse issue because if the cell that needs to be scrolled to is one of the first cells that are visible at load, it will highlight but if it's a cell that's off screen and it scrolls to that cell it will select but not highlight it.
ETA: cell reuse override:
- (void)prepareForReuse {
[super prepareForReuse];
self.selectionStyle = UITableViewCellSelectionStyleNone;
_cellEditing = NO;
_swipingToDelete = NO;
_editViewAnimated = NO;
}
In the event anyone else wants a custom highlight and selected state, the fix was:
if (highlighted || self.isSelected)
this will force it to jump inside this if block since when it's selected you want it the color of the highlight and selected state (assuming they're the same color)

UITableViewCell allways show some subview

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];

cell.accessoryView image appearing when it shouldn't for reusable cells only

I am using custom cells where I set two images, an accessoryView image and some custom buttons. The accessoryView image shows up initially. When the user swipes the row, i hide the accessoryView image and unhide the buttons. When the user 'unswipes' (or taps somewhere else), I hide the buttons and show the accessoryView. This all works fine, except when I do the following:
1)Load the screen. accessoryView image is there and buttons are hidden. Good.
2)Swipe a row. accessoryView disappears and buttons appear. Good.
3)Scroll down the table to reveal more cells (I'm using reusable cells). All new cells are shown with accessoryView and no buttons. Good, as expected.
4)Scroll back up to the original cell I swiped. I see both the accessoryView image and the buttons like this:
Here's some code:
In my custom cell object, I have:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected)
{
self.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"arrow-right-black.png"]];
UIImage *selectedRowImage = [UIImage imageNamed:#"table-selectedcellbg-red-45px-stretch.png"];
self.selectedBackgroundView = [[UIImageView alloc]initWithImage:[selectedRowImage resizableImageWithCapInsets:UIEdgeInsetsMake(selectedRowImage.size.height, selectedRowImage.size.width/2, selectedRowImage.size.height, selectedRowImage.size.width/2)]];
}
else
{
self.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"arrow-right.png"]];
self.selectedBackgroundView = [[UIImageView alloc]initWithImage:[[UIImage alloc]init]];
}
}
- (void)swipeCell
{
[self setSelected:NO animated:NO];
self.accessoryView = nil;
swipeButtons.hidden = NO;
}
- (void)unswipeCell
{
[self setSelected:NO animated:NO];
self.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"arrow-right.png"]];
swipeButtons.hidden = YES;
}
In my cellForRowAtIndexPath, I check to see if the cell was swiped:
if (self.indexPathSwipeButton != nil && self.indexPathSwipeButton.row == indexPath.row)
{
[cell swipeCell];
}
else
{
[cell unswipeCell];
}
return cell;
Any ideas?
You use [self setSelected:NO animated:NO]; in method (void)swipeCell and in method - (void)unswipeCell.
But in (void)swipeCell it should be [self setSelected:YES animated:NO];.
Hope it helps

Changing UIImageView from normal to highlighted doesn't work

I've got a UIImageView as the background of my UITableViewCell subclass. I have put an image in the Image field and a lighter version of that image in the Highlighted field. In my UITableViewCell subclass, I added code to set that ImageView to highlighted, but nothing changes...
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.backgroundImageView.highlighted = selected;
NSLog(#"selected: %d", selected);
}
I've verified that self.backgroundImageView is properly connected as an IBOutlet.
Just initialize the UIImageView like this:
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gray_arrow"] highlightedImage:[UIImage imageNamed:#"white_arrow"]];
If you need to keep it highlighted, in your tableview's didSelectRowAtIndexPath method, get the subview that represents your UIImageView and make it highlighted. Something like this:
UIImageView* imageView = [[[dataSource objectAtIndex:[indexPath row]] subviews] objectAtIndex:2]
[imageView setHighlighted:YES];
Set the UIImageView's in the other cells to :
[imageView setHighlighted:NO];

Resources