【问题标题】:Problems with TableView inside of ScrollViewScrollView 内部的 TableView 问题
【发布时间】:2017-01-23 04:39:04
【问题描述】:

我关注了你们提供的有用的 cmets,目前已经做到了这一点:我有一个带有标题单元格的 TableView。现在的问题是我只能显示一组数据,而 TableView 目前不会滚动(可能是因为只显示了一组数据。)

这是我的代码:

视图控制器:

    struct TableData {

        var section: String = ""
        
        var data = Array<String>()
        
        var dataS = Array<String>()
        
        init(){}

    }

    var data = Array<TableData>()
    var dataS = Array<TableData>()

    class MyCustomCell: UITableViewCell {

        @IBOutlet var label: UILabel!
        
        @IBOutlet var labelS: UILabel!
        
    }

    class MyCustomHeader: UITableViewCell {

        @IBOutlet var header: UILabel!
        
    }

    class TypeViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource {
        
        @IBOutlet var tableView: UITableView!
        
        @IBOutlet var scrollView: UIScrollView!

        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
            return data[section].data.count
        
        }
        
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCustomCell
            
            cell.label.text = data[indexPath.section].data[indexPath.row]
            cell.labelS.text = dataS[indexPath.section].data[indexPath.row]
            
            return cell

           
        }
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            
            let  headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader
            
            headerCell.header.text = data[section].section
            
            return headerCell

        }
        
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 50.0
        }
        
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            addSlideMenuButton()
            addItems()
            
            print(data)
            
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
func addItems() {

        var new_elements:TableData

        new_elements = TableData()
        new_elements.section = "Stuff"
        new_elements.data.append(obj41);
        new_elements.data.append(obj42);
        new_elements.data.append(obj43);
        new_elements.data.append(obj44);
        new_elements.data.append(obj45);
        new_elements.data.append(obj46);
        new_elements.data.append(obj47);
        
        data.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "More Stuff"
        new_elements.data.append(obj51);
        new_elements.data.append(obj52);
        new_elements.data.append(obj53);
        new_elements.data.append(obj54);
        new_elements.data.append(obj55);
        new_elements.data.append(obj56);
        new_elements.data.append(obj57);
        
        data.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "Netzach - Eternity"
        new_elements.data.append(obj61);
        new_elements.data.append(obj62);
        new_elements.data.append(obj63);
        new_elements.data.append(obj64);
        new_elements.data.append(obj65);
        new_elements.data.append(obj66);
        new_elements.data.append(obj67);
        
        data.append(new_elements)
        
        //Break
        
        new_elements = TableData()
        new_elements.data.append(objS0);
        new_elements.data.append(objS1);
        new_elements.data.append(objS2);
        new_elements.data.append(objS3);
        new_elements.data.append(objS4);
        new_elements.data.append(objS5);
        new_elements.data.append(objS6);
        new_elements.data.append(objS7);
        
        dataS.append(new_elements)
        
        new_elements = TableData()
        new_elements.data.append(objS11);
        new_elements.data.append(objS12);
        new_elements.data.append(objS13);
        new_elements.data.append(objS14);
        new_elements.data.append(objS15);
        new_elements.data.append(objS16);
        new_elements.data.append(objS17);
        
        dataS.append(new_elements)
        
        new_elements = TableData()
        new_elements.data.append(objS21);
        new_elements.data.append(objS22);
        new_elements.data.append(objS23);
        new_elements.data.append(objS24);
        new_elements.data.append(objS25);
        new_elements.data.append(objS26);
        new_elements.data.append(objS27);
        
        dataS.append(new_elements)
}

附上一些主线故事板的照片:

【问题讨论】:

  • 有什么问题?你想做什么?
  • developer.apple.com/library/content/documentation/WindowsViews/… 浏览这篇文章....支持同向嵌套滚动
  • 以正确的格式编辑您的问题。并添加您的原型单元屏幕截图。
  • 为什么你比较tableview喜欢,"tableView.tag == typeView.tag"。你可以直接比较"tableView == typeView"。
  • 表格视图是滚动视图。不要把它放在另一个里面。这简直是​​愚蠢!

标签: ios iphone swift uitableview


【解决方案1】:

请记住以下关于UITableView的要点

  1. UITableView 继承了UIScrollView 的属性,即UITableView 也像UIScrollView 一样位于下方,因此您无需使用UIScrollView 来专门滚动UITableView。如果你这样做,它的行为会很奇怪。

  2. cellForRow 中,您通过比较非标准格式的标签来创建参数tableView 到出口typeViewtypeView1 的条件。因为tableView.tag 可能会被更改并给你错误的输出。所以尝试使用以下格式

    if tableView == typeView { } else { } //tableView == typeView1

    UITableView 对象与婴儿车tableView 进行比较。

  3. cellForRow 方法从if-else 返回cell 所以你不需要写

return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

如果我调试您的 cellForRow 方法代码,那么您上面的这行代码永远不会执行。

首先尝试此标准并消除您的错误,然后发布您面临的问题和问题。

希望我以上的工作对你有所帮助。

编辑

您需要的输出将如下图所示。您不需要在单个 scrollview 中使用 2 个 UItableView 和这些 tableview。您可以使用 tableView 执行相同操作。

通过这个tutorial

【讨论】:

  • 对于第 3 点:也许编译器说 if-else if(不是 if-else)不能满足所有可能性,这就是为什么如果 tableView.tag 不是,他需要添加一个返回语句typeView.tag 而不是 typeView1.tag
  • 没有兄弟在最后一个 if-else 总是会有 else 所以把你的最后一个条件放在那里。
  • 如果那里只有if-else,那就对了。但是请看代码。在这一行中有} else if tableView.tag == typeView1.tag { 没有if-else - 有一个if - else if
  • 我还是有这个问题。我无法让我的 ScrollView 滚动。
  • @muescha 检查我的代码。只有两种可能性tableview 是type1 或者它是type2。并特别检查,tableview 为 `UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")` 返回了什么。有什么意思吗??我不这么认为。没关系,它返回单元格,单元格数据呢?您必须创建自定义类的对象并使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多