【问题标题】:Swift: how to deal with tableView when tableView width is wider than screen size?Swift:当tableView宽度大于屏幕尺寸时如何处理tableView?
【发布时间】:2018-05-02 00:22:41
【问题描述】:

我需要实现附图的场景。 当我尝试实现底部表单时,我遇到了一些问题。

底部表格有以下要求:

  1. 前 2 行应该贴在头部,并且不允许向上/向下滚动。我用table section head来实现。

  2. 所有其他行都可以向上/向下滚动并可点击。我用表格视图来实现

  3. 由于app支持较大的文字,当文字较大时,屏幕尺寸无法显示所有的tableView,所以在这种情况下,整个tableview应该支持水平滚动。

  4. 当 tableview 的行数据与值为“33342431”的“currentBitting”不同时,单个标签应显示背景颜色。

  5. 表格的每一列都应该对齐

以下是我如何实现的想法:

  1. 为了实现所有这些需求,我使用tableView来实现。数组项中的每个字符都使用标签来显示。

  2. 我使用最大宽度和常规宽度来定义每列的宽度

  3. 当文字字体大小改变时,我尝试设置底部tableView contentSize = "viewWillLayoutSubViews()" 中的当前新内容大小

  4. 我为section head添加了背景色,否则当底部tableview向上滚动时,它将与section head重叠,并且屏幕外的文本背景颜色不会呈现。所以我添加了节头背景颜色来避免这个问题。

目前,问题是水平滚动不起作用。很奇怪。

说实话,我尝试了很多方法:

  1. 底部表单先使用一个父scrollView,然后放一个子tableView。问题:顶部标题很难粘贴或垂直滚动​​不那么平滑。我粘贴了我的 scrollViewDidScroll 部分。有用。但是垂直滚动不是那么流畅。如果有人可以帮助改进它,我将不胜感激。
    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let recognizer = scrollView.panGestureRecognizer

        let distance = recognizer.translation(in: scrollView)

        let deltaHeight:CGFloat = parentScrollView.contentSize.height - parentScrollView.frame.height

        let goingUp = distance.y < 0 //going up
        if distance.y != 0 {

            if goingUp {
                parentScrollView.contentOffset.y = 0
                switch recognizer.state {
                case .began,.changed:
                    if bottomTableview.contentOffset.y < deltaHeight {
                        bottomTableview.contentOffset.y -= distance.y
                        recognizer.setTranslation(.zero, in: scrollView)
                    }
                case .ended:
                    if bottomTableview.contentOffset.y > deltaHeight{
                        bottomTableview.contentOffset.y = deltaHeight
                        UIView.animate(withDuration: 0.1, animations: {
                            scrollView.layoutIfNeeded()
                        })
                    }
                default:
                    break 
                }  
            }
            else{
                switch recognizer.state {
                case .began,.changed:
                    if bottomTableview.contentOffset.y > 0 {
                        bottomTableview.contentOffset.y -= distance.y
                        recognizer.setTranslation(.zero, in: scrollView)
                    }
                case .ended:
                    if bottomTableview.contentOffset.y < 0 {
                        bottomTableview.contentOffset.y = 0
                        UIView.animate(withDuration: 0.1, animations: {
                            scrollView.layoutIfNeeded()
                        })
                    }
                default:
                    break
                } 
            }
        }
    }


  1. 集合视图以实现。但是我不知道如何选择一整行来获取数据以及如何粘贴顶部标题

有没有人有更好的想法来实现这个案例?非常感谢您的帮助。

要在表格视图中使用的数据是:

let extensionCodeArray = ["Position", "Current Bitting", "11407", "10253", "10492", "14195", "11665", "10547", "12332", "12536", "10211", "11034", "10725", "11036", "12928"]
let extensionResultBitting = ["12345678", "33342431", "33133244", "33243134", "32343134", "33134243", "31343234", "33342134", "31344233", "34342133", "31334342", "33134342", "34343132", "34231343", "32431343"]

【问题讨论】:

  • 您可能需要查看 UICollectionView:developer.apple.com/documentation/uikit/uicollectionview
  • UITableView 只支持单向滚动。尝试在UIScrollView 上添加表格视图。
  • 我试过了。但是使用我的scrollViewDidScroll,tableView 不能平滑地垂直滚动,也没有tableView 的那种新鲜效果。我已经粘贴了我的代码实现。欢迎任何意见

标签: ios swift tableview


【解决方案1】:

我解决了这个问题。思路如下:

  1. 定义一个collection view,设置它的numberOfItemsInSection = 1
  2. 对于collectionView,设置它的
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize --> return CGSize(width: contentSize.width, height: collectionView.frame.size.height)

cotentSize 是预先为最终形式计算出来的

  1. 自定义UICollectionViewCell,其中实现tableView数据源和delegate。用于tableView 的所有datasource 都来自UICollectionViewCell 中定义的protocol。外侧collectionViewdelegateprotocol

完成。

【讨论】:

    【解决方案2】:

    对于您尝试的第一个解决方案。

    UITableViewUIScrollView 的子类,这意味着UITableView 也有scrollViewDidScroll(_ scrollView: UIScrollView) 事件。

    要解决此问题,您只需执行以下操作。给tableView或scrollView设置一个标签来识别它。
    1. 例如,要识别scrollView,请在初始化scrollView 之后添加以下行。

    scrollView.tag = 101  
    
    1. func scrollViewDidScroll(_ scrollView: UIScrollView)
        func scrollViewDidScroll(_ scrollView: UIScrollView) 
         {
            if scrollView.tag == 101 { 
              // Triggered by scrollView
             } else { 
             // Triggered by tableView 
            }
    
         }
    

    【讨论】:

    • 谢谢@JsW。我认为您的想法是向上滚动时,让 tableView 滚动。向下滚动时,让滚动视图滚动。问题是向上滚动的时候,还是父scrollview向上滚动,所以stick head会随着父scrollview的移动而消失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多