简体   繁体   中英

TableView horizontal scrolling according to cell width

I have table view inside scroll view which will scroll according to table view cell width.I have set the scroll view from top,bottom,leading and trailing.Then I have given the same constraints to view inside the scroll view including centerX and centerY.Then for horizontal scrolling I have set the trailing and centerX priority to 250.But my table view is not scrolling.I want to set it through autolayout.

I want the table to scroll horizontally if label increases inside the table view cell.

This is my view hierarchy

在此处输入图片说明

@Zahid I made a public example on github with what I wrote about below:

https://github.com/amichnia/stack-54735700

Original answer:

As it was already said, table views do not scroll horizontally in general. But no one said you can't put table view inside UIScrollView that scrolls horizontally . There would be some challenges here though:

  1. I don't think autolayout with autoresizing cells would work good.

  2. You need to figure out scrollview/tableview gesture recognizers order. You can use UIGestureRecognizer delegate methods to resolve their collisions

  3. You need to determine width of the table view. You can do it with old nasty way:

    • compute size of every cell first,
    • take max of these sizes
    • set it to table view width
    • reload data

Is it worth the hassle? That's your call to take.

With your setup (I see scroll view as root view) the simplest (but not best) option would be to disable scrolling on table view, enable scrolling both directions on scroll view, and just update whole table view size. It will work well only with small amount of cells though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM