When the user taps the accessory view, the profile image in the UITableViewCell gets darkened by a black view that gets added on as a subview.
Here is what the cell looks like with the black subview:
Here's the issue: When I tap on another cell, the subview gets removed from the first cell and added to the second:
I would like to keep the subview for all cells that have been tapped.
Here is the code in which I handle that functionality:
self!.profileImageBlackView.cornerRadius = cell.followUserImage.frame.height/2
self!.profileImageBlackView.frame = cell.followUserImage.frame
cell.followUserImage.addSubview(self!.profileImageBlackView)
cell.followButton.hidden = false
For some reason, the follow button gets added to both cells, but the "profileImageBlackView" gets moved from cell to cell depending upon which one was activated.
You cannot display the same view in different cells. If you add the view to another cell it's removed from the first one. You must create a separate view for each cell.
Related
I have a question - how to add a view as a subview of UITableViewCell but on top of all other views?
Here is an example of what I'm trying to achieve:
The image should scroll with the cell - save it's position relatively to scroll view (table view in this case).
If the cell is to small in height then the emojis overlay will be cut by the cell on top of current one, so how can I add this emojis overlay as a subview but at the same time display it on top of every cell? I've tried bunch of variants for now and nothing is working for me.
Just adding as a subview of the current window is not working for me because this overlay should scroll with the cell.
Tried code:
view.layer.zPosition = 5
cell.contentView.insertSubview(view, aboveSubview: cell.contentView)
cell.contentView.bringSubview(toFront: view)
UIApplication.shared.keyWindow?.addSubview(view)
Try to set cliptobouds = false for your cell.contentview and cell itself.
Otherwise if it is single overlay which you want to be on top of every cell then better implement UISrollViewDelegate and position the overlay accordingly.
I have multiple collection views on one screen
In collectionView One i have normal cell like Square
In collectionView two i have group cell e.g same cell but multiple surrounded by a border of Group
I need to Enable Drag and Drop between One Collection View to Another.
I am using swift 3.0.
You can use this one from GitHub:
https://github.com/mmick66/KDDragAndDropCollectionView
And here is a nice guide for it:
http://blog.karmadust.com/drag-and-drop-between-uicollectionviews/
Drag from collectionView2:
Create an independent and drag-able instance of the cell with the same data and place it above the cell to drag
In Collectionview2, set the cell.isHidden = yes. This way the cell is not visible, but its empty space remains.
When the drag-able cell is significant dragged away from the original position, remove the original cell from the collectionView animated.
Drag to collectionView1:
When the drag-able cell is above collectionView1 and is dropped:
insert (animated) a cell to the target position in collectionView1 and set it hidden
move drag-able cell above the inserted (but hidden) cell
set target cell to isHidden = false
remove drag-able cell from superview
I'd like to add view beside tableviewcell when user click table view cell.
before user click
---cell1---
---cell2---
after user clicked
---cell1---
---detailview---
---cell2---
How to do that?
If you are looking for something like another view appearing below the tapped cell and not an expanding tapped cell,you can insert a tableview cell just after the tapped cell.Use insertRowsAtIndexPaths and insert the cell.You can create this cell as a custom cell.
There is 2 options to do that:
- To make detailView as another cell, just insert below the cell user clicks
- Cell add the detailView initially. And make a switch to show it or hide it.Just because cell in table view is reused, we should not add subview in a cell already in table view, which will cause some problem in reusing.
I have a UITableView of cells where one cell contains a UITableView. Selecting that row that contains the table pushes a new view onto the screen with a list of items. The user selects a list of items and then presses 'back' to pop that view. The parent view looks at the number of items selected and the height of the cell is supposed to adjust to show all items selected.
What happens is that when the view is reshown, the listed items extends into the cell below. If I scroll that cell off the screen, then back to it, the cell is then the right height showing all items correctly.
I've tried several things such as putting code into viewWillAppear of the main table like [self.view setNeedsDisplay] and [self.view setNeedsLayout], but it doesn't work.
I can't seem to find a way to make that cell redraw to it's right size without scrolling the table once the view appears.
Is there some other method to force the redraw before it actually appears?
try:
NSIndexPath *theIndexPath = [NSIndexPath indexPathForRow:cellRow inSection:cellSection];
NSArray *theIndexPaths = [NSArray arrayWithObject:theIndexPath];
[table reloadRowsAtIndexPaths:theIndexPaths withRowAnimation:NO];
What are you currently using to redraw the cell? Just waiting for it to redraw itself when it goes off screen and back on? Or are you using something like [table reloadData];?
I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.