Changing the contentView when an Accessory button is tapped - ios

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.

Related

Why does UICollectionView not scroll with UIButtons inside it?

Given the following scenario:
There is a UICollectionView containing UICollectionViewCells (naturally). Each UICollectionViewCell contains a UIButton with a target action associated with it.
Problem:
The UICollectionView does not scroll upon swipe/drag. The UIButtons instead intercept touches, and make it difficult to drag the UICollectionView.
Additionally:
The UIButtons are set to UIControlEventTouchUpInside
The UICollectionView is set to canCancelTouches = YES
**Why are the UIButtons inside UICollectionViewCells preventing the UICollectionView from responding to drag/swipe gestures? **
The reason that the UICollectionView doesn't scroll properly is because the UIButtons are intercepting and rerouting the responder chain.
Solution:
Remove the UIButtons from the UICollectionViewCell.
Instead use the delegate method for UICollectionViewDelegate :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Explanation:
When we added a UIButton to the UICollectionViewCell, we were thinking that the best way to capture a tap was to add a button to the cell. However, by adding the UIButton, we broke the responder chain.
We don't need the button inside the UICollectionViewCell, because the UICollectionViewCell already detects tap events with it's delegate method :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Use the provided method to detect taps on a collection cell, and replace the UIButtons with UIImageView-s or similar.
We don't need the event handing of a button when working with collection cells.

Perform segue with clicked on a button on UICollectionCell

I want to perform a seque from a UICollectionViewCell inside a UICollectionView. There is a UIIMage that is a subview of UICollectionView. There is a button which is a peer view of UICollectionViewCell. I have used the workarround of a transparent button on top of UIIMage to capture touch events on this cell. Now I want to perform a segue when the user clicks this Image on the collection cell. The problem is that I dont know which cell's button triggered the segue and handle it in prepare for segue.
The button triggers an IBAction where the sender is this button. How do I know which cell is its UICollectionCell underneath this transperent button ?
add tags to your buttons and perform different segues depending on the tags. Another way - without buttons would be to add tap gestures to your UIImages and perform segues depending on which image has been tapped ...
Found the solution: Its similar to the one solved for tableview (of course given UICollectionView is a repackaged table view) posted here.
The solution is to use
-(void)buttonPressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
...
}

UIScrollview in a Custom UICollectionViewCell Not Working

I'm trying to get a UIScrollView to work correctly inside of a UICollectionViewCell.
The custom cell is being loaded in via a xib file and is a subclass of UICollectionViewCell. I had problems getting other controls working, such as a button and a gesture recognizer since the UICollecitonView doesn't seem to be passing any touches to the cells, but I got around those with gesture recognizers on the UICollectionView itself. The one remaining issue I have is the UIScrollViews...
The UICollectionView scrolls horizontally, and the UIScrollView in the cells scroll vertically. I've tried using a UIPanGestureRecognizer to scroll them, but that seems to disable the UICollectionView's ability to scroll.
Anybody have any thoughts?
EDIT: Got it!
So I had converted to a collection view from a previous third party library we were using before iOS6. Turns out the problem was with the xib files we were using for the cells. With the library before, the cells were just subclasses of UIViews. I changed the classes to subclass UICollectionViewCell, and updated the Custom Class. Turns out this was not enough. In order for touches to get passed to the cells I needed to actually had to drag in a new UICollectionViewCell from the Object library, copy over all the subviews and reconnect the IBOutlets. After this, it worked!
I fixed this in my code by making sure resizing of the scroll view happens on main thread.
My collection view is using nsfetchedresultscontroller that is using block calls to refresh selected cells. On the first time a cell was selected the scrollview would not scroll. However if you clicked on another cell and clicked back it would work fine. The initial load of the cell seemed like size calc might not be where it needed to be on main thread to affect behavior.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
// resizeScrollViewMethod should be where scrollview content size > scroll view frame.
[cell resizeScrollViewMethod];
});
return cell;
}
- (void) resizeScrollViewMethod {
//Do your scrollview size calculation here
}
No problem with UISCrollView in UICollectionViewCell. Use StoryBoard and you'll scroll OK.
UIScrollView overlay UICollectionViewCell, so that didSelect work only when tap outside ScrollView and inside Cell (scrollView.frame < cell.frame).
If you want to implement tap or other gesture, just add it to UIScrollView in awakeFromNib
Refer code:
https://github.com/lequysang/github_zip/blob/master/CollectionViewWithCellScrollViewStoryBoard.zip

How to hide the particulars uitableViewCell webview in when i click on that particuar cells button uitableview?

In tableviewcells i have a buttons and webViews, with some array count.so when i tap on particular tableViewcells button action. i have to hide that particular tables cells webview?
You can do it like this, In UIButton action, you will get cell index if you've given tag to your button, using that tag, you can make cell object with NSIndexPath for the cell you've tapped, hide UIWebView subviews within that cell, you may need to set its height base on some condition which you've made in heightForRowAtIndexPath of UITableView. Another way to get the cell is, [button superview]; here you'll get containerview for particular cell, and apply the same logic, this would only work if you've added button and webview in contentview.
[cell.webView setUserInteractionEnabled:NO];
Make the webView can't get the user touch focus, so that the cell will get the user interaction : )

UIButton in custom UITableViewCell stop working after rotation

Problem:
I used custom UITalbeViewCell that contains two buttons, they work fine in the portrait orientation. After the rotation, they all stop responding to the button touch up inside function. Some people having problems that their buttons couldn't drew correctly after rotation. Mine looked fine since the buttons are showing in the right places after rotation, but they do not respond to the button press anymore.
For this specific view in my app, I used a UIPageController to implement multi pages in a view, and for the view (name it EmbeddedView for now) embedded in the page's scroll controller, there is a UITableView that contains custom UITableViewCell. Custom table view cell only has a nib, the file's owner is EmbeddedView.
in EmbeddedView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*===== This is the most memory efficient way of creating table view cells =====*/
static NSString *CellIdentifier = #"CellIdentifier";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[self customTableCellNib] instantiateWithOwner:self options:nil];
cell = [self customTableCell];
[self setCustomTableCell:nil];
}
}
What I tried:
I created another nib file for the custom table view cell and used it in -cellForRowAtIndexPath(), I checked the orientation and dynamically create the cell by using different nib, no luck.
I added [tableview reloadData] in -didRotateFromInterfaceOrientation(), didn't do anything.
Would someone point me to the right direction please? Any help is appreciated.
This is the table view Autosizing in IB:
It looks right but the buttons are not working
Update: I tried to specify different Autosizing masks in IB for the table view, and the results are showing below:
<1>
<2>
<3>
<4>
Have you checked how the superview is being resized?
Check if the superview has 'clip to bounds' checked. If it is not check it. That will make the view clip its contents so you see if it is resizing ok.
I'd say the superview is not sizing correctly and because of that the touch events are not well delivered also.
EDIT - So this was the tip that could let the OP reach the solution:
What I normally do in cases like of unexpected behavior in resizing and such is to change every view in the hierarchy to a different, well recognizable, color. Right now you have view A and view B with the same background color (or clear) and you don't see if view B is resizing well. Good luck.

Resources