Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

iphone - Scrolling two UITableViews together

I have a weird design case in which to implement I need to place two UITableViews on the same view. I would just use sections, but I need an index on the second table.

I'd like them to both scroll together like they were actually two sections of the same table view. Is this possible?

Here's a basic mockup illustrating why:

mockup

As far as I understand it, you cannot move the index. Also, when you add an index to a table, it appears over the entire tableview, not just one section.

If I have to implement this as two table views, they both need to scroll as if they were one. If it's possible to do this using sections, that's even better.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update

After seeing your update my old answer is not very good but I'll leave my previous answer for anyone else who wants it.

New answer - It depends how you want the tables sync'd.

If you know

  1. Which cells are in the top 5
  2. How tall each cell is
  3. The offset the table view

Then you can calculate which cell is visible at the top of the top 5 table. You can use this information to scroll the bottom table to the correct index.


Old answer

I'm with the other guys not sure why you would want to or if I am misinterpreting but it's pretty simple.

UITableView is a subclass of UIScrollView so you can set yourself as the UITableViewDelegate and override

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    UITableView *slaveTable = nil;

    if (self.table1 == scrollView) {
        slaveTable = self.table2;
    } else if (self.table2 == scrollView) {
        slaveTable = self.table1;
    }

    [slaveTable setContentOffset:scrollView.contentOffset];
}

This gives me something that looks like this:

enter image description here

The top UITableView is self.table1 and the bottom is self.table2 if either table is scrolled the scrolling is mirrored in the other table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...