UITableView get UIScrollView for customization - iphone

I have category for UIScrollView for customization
I need get UIScrollView.
I try
UIScrollView *scroll = nil;
for (UIView* subview in self.tableView.subviews) {
NSLog(#"%i", self.tableView.subviews.count);
if ([subview isKindOfClass:[UIScrollView class]]) {
scroll = (UIScrollView *)subview;
//some code
}
}
But it doesn't work. How I can get ScrollView? Thanks

UITableView is a subclass of UIScrollView, not a subview (or vice-versa as your code tries), so you should just use the table view directly.

UIScrollView is not subview but UIScrollView is superclass of UITableView you can call method or get variable of UIScrollView by through UIScrollView, If you want to get UIScrollView you can use by this
var scroll = self.tableView as UIScrollView
//some code
Or you want to compare you can use
if scrollView == self.tableView {
//some code
}

Related

how to expand the dynamically created UIviews on clicking of the UIButton

I have created dynamic UIView with scrollview and UIView is having subview UIbutton.
my requirement is on clicking of the UIButton I need to show one more UIView in between the dynamic UIviews.
I need to know how to change the dynamic UIView frame size dynamically on clicking of the UIbutton.
Thanks in advance.
here is the samples
NSArray *subViews = [createIssueScroll subviews];
NSLog(#"%#",subViews);
NSInteger tag=[sender tag];
NSLog(#"%d", tag);
UIView *vw = [createIssueScroll viewWithTag:tag];
[self createIssueSubView:[vw frame]];
UIView *sVW = [createIssueScroll viewWithTag:(tag+1)];
NSLog(#"%#", sVW);
float y;
y = sVW.frame.origin.y;
sVW.frame = CGRectMake(0, y, sVW.frame.size.width, sVW.frame.size.height);
NSLog(#"%f", y);
code explanation
i have created button through programatically, i will explain in detail above code
In first line i have taken all the scrollview subviews(dynamically created UiViews)
i have button tag value
and add a method of UIsubviews(this subview is displaying on clicking of ui button)
suppose i have click dynamically created first Uiview button, need to show subview after the first dynamic UIview and second dynamic UIview is push to down side.
Find subViews by using foreach
UIView *firstView;
UIView *seconfView;
for(id currentview in scrollView.subviews)
{
if([currentview isKindOfClass:[UIView class]])
{
UIView *view = (UIView *)currentview;
if(view.tag == 1) //1 is your view tag
{
firstView = view;
}
if(view.tag == 2) //2 is your view tag
{
seconfView = view;
}
}
}
Now you can use first and second views :)

Can i reuse UIView?

I have UIView in my storyboard with outlet mynewView and i want it display with totally different content. And do it many times.
My UIView contains many objects, i load it from internet. But every time i do
NSArray *viewsToRemove = [view1 mynewView];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//Let's try to remove all possible views..
viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//here i create mynewView
mynewView = [self createnewRandomView];
//And finally add the View we have created.
[self.view addSubview:mynewView];
i monitor the memory consumption and i see it grows every time i add new UIView. Even when i remove all superviews. I guess they remain in memory.
Before i change the view i remove from superview all possible views, but they still stay in memory.
Xcode is set to use ARC so i can't release it.
Is there any good way to reuse a UIView?
Thanks
[self.view addSubview:mynewView];
Your newView was added as subview of self.view.
Your above code does not use for remove yourVewView in self.view.
Please try this code before adding new view:
for (UIView *view in self.view) {
if([view isKindOfClass:[YourNewViewClassName class]])
{
[view removeFromSuperView];
break;
}
}
As you remove the subviews, set them to nil as you go, like so:
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
v = nil;
}
For more information, see for example Arc: Setting references to nil, for multiple buttons

How to remove a subView from detailView?

I have a table in my masterView and in my detailView i have some imageView, based on different row selection in my masterView i have to remove ImageView and add some other views. Im unable to remove the ImageView.... any help ??? thanks in advance
try:
for (UIView *view in masterView.subviews){
if (view == yourImageView) {
[view removeFromSuperview];
}
}
first you provide tag values other than 0 to all your imageview
and then
for (UIView *sub in [self.view subviews]) {
use tag to find proper view and remove it from superView
}

How to find out which other UIScrollView interferes with scrollsToTop?

I have several view controllers with one or multiple scrollviews. Although I have explicitly set the scrollsToTop flags in view controllers with more than one scroll view, some scroll views refuse to scroll up when I tap the status bar.
After pushing another view controller and popping it the gesture sometimes works in the view it previously hasn't.
It's very confusing and I just don't know what the problem is. How can this issue effectively be debugged? Is there a global (private) notification for the status bar tap so I could scroll the views manually?
I have used code like the following to debug this scenario, before:
- (void)findMisbehavingScrollViews
{
UIView *view = [[UIApplication sharedApplication] keyWindow];
[self findMisbehavingScrollViewsIn:view];
}
- (void)findMisbehavingScrollViewsIn:(UIView *)view
{
if ([view isKindOfClass:[UIScrollView class]])
{
NSLog(#"Found UIScrollView: %#", view);
if ([(UIScrollView *)view scrollsToTop])
{
NSLog(#"scrollsToTop = YES!");
}
}
for (UIView *subview in [view subviews])
{
[self findMisbehavingScrollViewsIn:subview];
}
}
Depending on how many UIScrollViews you find, you can modify that code to help debug your particular situation.
Some ideas:
Change the background colors of the various scrollviews to identify them on screen.
Print the view hierarchy of those scrollviews to identify all of their superviews.
Ideally, you should only find a single UIScrollView in the window hierarchy that has scrollsToTop set to YES.
I changed the accepted answers into swift for convenience:
Swift 5
func findMisbehavingScrollViews() {
let topView = UIApplication.shared.windows.first { $0.isKeyWindow }
findMisbehavingScrollViewsIn(topView!)
}
func findMisbehavingScrollViewsIn(_ topView: UIView) {
if let topView = topView as? UIScrollView {
if topView.scrollsToTop {
print("Found UIScrollView: \(topView)")
}
for nextView in topView.subviews {
findMisbehavingScrollViewsIn(nextView)
}
}
}

scrollViewWillBeginDragging not responding on a second UIScrollView

I have a probably nub question but i just can't get around this.
I have a UIScrollview on the top of the screen, below it there is a UITableView.
both are on my InterfaceBuilder, not done programmatically
and declared on my .h as:
#interface RootViewController : UIViewController <UIScrollViewDelegate, IconDownloaderDelegate>
{
IBOutlet UIScrollView *scrollview;
}
etc.
Inside the UIScrollView i have a long tabbar also in IB mi code is as follows for this:
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(1100, 29)];
Everithing works perfectly the tabbar works and moves etc.
the problem is that when implementing scrollViewWillBeginDragging
it only registers the scrolling on my UITableView and not on the scrollview.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(#"done");
}
What am i doing wrong??
Thanks in advance!
You have to set the delegate to the class where you implement the delegate methods.
scrollview.delegate = self;
add [scrollview SetDelegate:self]; in the place you add scrollview