In my app, I have two tableViews side by side. When the user scrolls on, I would like the second one to scroll at the same time, so it looks almost like one table with two different columns. I'm a bit lost on how to go about doing this, any suggestions?
Thanks,
Greg
Conveniently, UITableView is a subclass of UIScrollView. There exists a UIScrollViewDelegate, which has this method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
If you implement that method, you can get the contentOffset property of the scrollView argument. Then, you should use
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
and set the new content offset. So something like this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIScrollView *otherScrollView = (scrollView == self.tableView1) ? self.tableView2 : self.tableView1;
[otherScrollView setContentOffset:[scrollView contentOffset] animated:NO];
}
You can cast to a UITableView if you'd like, but there's no particular reason to do so.
You'll want to look into the UIScrollViewDelegate - say you've got two scroll views, A and B.
Use the scrollViewDidScroll delegate method of scroll view A to get the offset, and then in the same method call setContentOffset on scroll view B, passing in the value you get from the delegate.
It actually shouldn't be more than 2-3 lines of code once you've set-up your delegate methods.
also, the tableview that got scrolled by the user should not be sent setContentOffset: message in scrollViewDidScroll, since it will get the app into endless cycle. so additional UIScrollViewDelegate methods should be implemented in order to solve the problem:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
beingScrolled_ = nil;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if(beingScrolled_ == nil)
beingScrolled_ = scrollView;
}
and modifying Inspire48's version scrollViewDidScroll: accordingly:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIScrollView *otherScrollView = (scrollView == self.tableView1) ? self.tableView2 : self.tableView1;
if(otherScrollView != beingScrolled)
{
[otherScrollView setContentOffset:[scrollView contentOffset] animated:NO];
}
}
where beingScrolled_ is an ivar of type UIScrollView
UITableView is a subclass of UIScrollView. Swift solution:
extension yourViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let otherScrollView = (scrollView == tableViewLeft) ? tableViewRight : tableViewLeft
otherScrollView?.setContentOffset(scrollView.contentOffset, animated: false)
}
}
And stop the bounce of both the tables
tableViewLeft.bounces = false
tableViewRight.bounces = false
If you want to make vertical scroll Indicators go, write the below code
tableViewLeft.showsVerticalScrollIndicator = false
tableViewRight.showsVerticalScrollIndicator = false
in swift scroll two uitableviews symmetrically:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if tb_time1 == scrollView {
tb_time2.contentOffset = tb_time1.contentOffset
}else if tb_time2 == scrollView {
tb_time1.contentOffset = tb_time2.contentOffset
}
}
Related
I have UIScrollView with a lot of rows (~100) and I implemented dequeueReusableRow method for fast allocating and adding my subviews (rows). Everything work fine, but if I scroll very fast with decelerate some view don't added to scrollView on time only later.
- (UIView *)dequeueReusableRow
{
UIView *view = [reusableRows anyObject];
if(view)
{
[[view retain] autorelease];
[reusableRows removeObject:view];
}else{
view = [[UIView alloc] init....
}
return view;
}
- (void)addVisibleRows
{
UIView *row = [self dequeueReusableRow];
row.frame = ....
[scrollView addSubview:row]
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self addVisibleRows];
[self removeInvisibleRows];
}
Please, don't propose me use UITableView because structure of accordion looks like:
section
- section
-- section
--- row
- section
section
- row
dequeueReusableRow is not part of UIScrollViewController, so I assume dequeueReusableRow is something you implemented yourself. If so, can you show that code? It is likely where we can help with any inefficiencies.
Also, if your scrollview contains rows, why not just use a UITableView which will do much of the work for you? I know you asked not to propose it - can you explain why you need to use a ScrollView so we can help you better?
It's very hard to tell from that code snippet. Some more details would be appreciated.
One minor suggestion in the meantime: Call removeInvisibleRows before addVisibleRows
Is there any way to add multiple UIScrollView's on a single UIView?
I made the UIView 2 parts. On the first part of view, I want to add one UIScrollView, and on the other, I want to add a second scrollview. The problem I'm facing is when I'm trying to zoom on the first, the second scroll is also responding.
How can I avoid that?
You should differentiate your scroll view by its tag property. Like :
assign tag to your scroll views
scrollView1.tag = 2001;
scrollView2.tag = 3001;
And then
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView
{
if (myScrollView.tag == 2001)
{
//do stuff with scrollView1
}
else if (myScrollView.tag == 3001)
{
//do stuff with scrollView2
}
}
Use this Delegate method:-
Delegate methods send with it the object that sent the message (the UIScrollView in this case). So, all you have to do is check that against your instance variables of scrollView1 and scrollView2.
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView {
if (myScrollView == scrollView1) {
//do stuff with scrollView1
} else if (myScrollView == scrollView2) {
//do stuff with scrollView2
}
}
Is there a way to alter the frame of the "swipe" [DELETE] button used on UITableViewCells? Currently its centred vertically within the cell, but if possible I would like to move it down to the cyan guide show.
If you are looking for a strongly true way to solve this problem then you should to subclass of UITableViewCell and override the all state handling methods for correct drawing your own delete button (do not call super in those methods). But there is another easy way:
#implementation CustomCell
- (void)layoutSubviews {
[super layoutSubviews];
if (self.showingDeleteConfirmation) {
if ([self.subviews count] < 4) return;
UIView *delBtn = [self.subviews objectAtIndex:3];
delBtn.frame = CGRectOffset(delBtn.frame, 0, 10);
}
}
#end
Instead of didTransitionToState:,
How about using the -(void)willTransitionToState: and setting the frame of the editingAccessoryView?
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
if (state == UITableViewCellEditingStyleDelete)
{
NSInteger num = 10;
UIView.frame = CGRectMake(UIView.frame.origin.x,UIView.frame.origin.y - num,
UIView.size.width,UIView.size.height);
}
}
Try changing the frame for Accessory View
I was wondering how to find out if a subview (in my case pageShadowView) has already been added to my view.
I've come up with this, but it doesn't really work:
if ([pageShadowView isKindOfClass:[self.view class]]) {
[self.view addSubview:pageShadowView];
}
Also, I'm still confused about the self.-thing. I know that this has to do with making clear that we are talking about the view of the current ViewController ... but do I really need it if there (1) are no other ViewControllers or (2) if it doesn't really matter because if I ever wanted to refer to another viewController, I'd make sure to call it?
I'm sorry if this is all very basic, but I'd be very grateful for your comments.
Here:
BOOL doesContain = [self.view.subviews containsObject:pageShadowView];
And yes, you need this self. There is no explicit ivar "view" on UIViewController. The self.view statement is actually a call on method [self view] which is a getter for UIViewController's view.
Give it a unique tag: view.tag = UNIQUE_TAG, then check the container view for existence:
BOOL alreadyAdded = [containerView viewWithTag:UNIQUE_TAG] != nil;
you can find a sub view like this
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIView class]])
{
//here do your work
}
}
There's one more way to find, in Swift: isDescendant(of view: UIView) -> Bool or in Obj-C: - (BOOL)isDescendantOfView:(UIView *)view
Swift:
if myView.isDescendant(of: self.view) {
//myView is subview of self.view, remove it.
myView.removeFromSuperview()
} else {
//myView is not subview of self.view, add it.
self.view.addSubview(myView)
}
Obj-C:
if([myView isDescendantOfView:self.view]) {
//myView is subview of self.view, remove it.
[myView removeFromSuperView];
} else {
//myView is not subview of self.view, add it.
[self.view addSubView:myView];
}
SWIFT VERSION:
let doesContain = self.view?.subviews.contains(pageShadowView)
best and easy way to find that your view is exist or not , there are many way like you check a view is contain or not in super view or just view some time this method is failed because if the Uiview is already remove then error occur,
so code is here :
here errorView is my UiView
errorView.tag = 333
if ( self.view?.viewWithTag(333) != nil ){
print("contain")
}
else {
print("not contain")
}
To add to what coneybeare said, you could do the following. If you set your object.tag=100;
if ([self.view.superview viewWithTag:100] == nil){ //if statement executes if the object with tag 100 in view.superview is absent (nil)
if ([self.view viewWithTag:100] == nil){ //if statement executes if the object with tag 100 in view (not superview) is absent (nil)
add a retain value of the view
then check the retain value
if > 1 , then exist , if perfect should be 2
then release it once
Similar to this question I have a custom subclass of UITableViewCell that has a UITextField. Its working fine except the keyboard for doesn't go away when the user touches a different table view cell or something outside the table. I'm trying to figure out the best place to find out when something outside the cell is touched, then I could call resignFirstResponder on the text field.
If the UITableViewCell could receive touch events for touches outside of its view then it could just resignFirstResponder itself but I don't see any way to get those events in the cell.
EDIT: I tried this (below) in my UITableViewCell subclass but it doesn't work, I think because touchesBegan:withEvent: doesn't get called if the event was handled by a control. I think I need to catch the events before they get send down the responder chain somehow.
The solution I'm considering is to add a touchesBegan:withEvent: method to the view controller. There I could send a resignFirstResponder to all tableview cells that are visible except the one that the touch was in (let it get the touch event and handle it itself).
Maybe something like this pseudo code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = // TBD - may need translate to cell's coordinates
for (UITableViewCell* aCell in [theTableView visibleCells]) {
if (![aCell pointInside:touchPoint withEvent:event]) {
[aCell resignFirstResponder];
}
}
}
I'm not sure if this is the best way to go about this. There doesn't seem to be any way for the tableviewcell itself to receive event notifications for events outside its view.
EDIT2: I thought I had an answer (I even posted it as an answer) using hitTest:withEvent: but that didn't work out. It doesn't always get called. :-(
[Edited: removed previous attempt which didn't always work, this one does]
OK, I finally figured a solution that fully works. I subclassed UITableView and overrode the hitTest:withEvent: method. It gets invoked for all touches anywhere in the table view, the only other possible touches are in the navbar or keyboard and the tableview's hitTest doesn't need to know about those.
This keeps track of the active cell in the table view, and whenever you tap a different cell (or non-cell) it sends a resignFirstResponder to the cell going inactive, which gives it a chance to hide its keyboard (or its datepicker).
-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
// check to see if the hit is in this table view
if ([self pointInside:point withEvent:event]) {
UITableViewCell* newCell = nil;
// hit is in this table view, find out
// which cell it is in (if any)
for (UITableViewCell* aCell in self.visibleCells) {
if ([aCell pointInside:[self convertPoint:point toView:aCell] withEvent:nil]) {
newCell = aCell;
break;
}
}
// if it touched a different cell, tell the previous cell to resign
// this gives it a chance to hide the keyboard or date picker or whatever
if (newCell != activeCell) {
[activeCell resignFirstResponder];
self.activeCell = newCell; // may be nil
}
}
// return the super's hitTest result
return [super hitTest:point withEvent:event];
}
In my UITableViewCell subclasses that have a UITextField, I add the following code to get rid of the keyboard (or date picker, which slides up just like the keyboard):
-(BOOL)resignFirstResponder
{
[cTextField resignFirstResponder];
return [super resignFirstResponder];
}
Yay!
I think you're on the right track, but touchesBegan:withEvent: is a UIResponder method, so you'd actually have to override it in a UIView subclass rather than in your UIViewController subclass. Your options are:
If you're already subclassing UITableViewCell, override touchesBegan:withEvent: there.
If you're using a standard UITableViewCell, implement tableView:didSelectRowAtIndexPath in your UITableView's delegate.
That is a very good solution, the best I've found on the net. The only glitch I've discovered is that if you go from one cell with a textfield to another, the keyboard dismisses and reappears resulting in a jerky type animation.