Ios 如何防止UITableViewCell';使内容在水平滚动时不会消失 一般说明

Ios 如何防止UITableViewCell';使内容在水平滚动时不会消失 一般说明,ios,uitableview,horizontal-scrolling,Ios,Uitableview,Horizontal Scrolling,我有3个UITableView,它们的空间不大,必须水平滚动,所以我使用UIScrollView作为标题,然后使用它们的contentOffset使它们一起滚动。每个表都有一个带有自己自定义类的原型单元 这是滚动视图的结构: scrollView //The next objects apply only to the tables with a predefined number of labels (2 of 3) view label1 la

我有3个UITableView,它们的空间不大,必须水平滚动,所以我使用UIScrollView作为标题,然后使用它们的contentOffset使它们一起滚动。每个表都有一个带有自己自定义类的原型单元

这是滚动视图的结构:

scrollView
    //The next objects apply only to the tables with a predefined number of labels (2 of 3)
    view
        label1
        label2
        label3
        label4
这是TableView的结构:

tableView
    tableViewCell
        contentView
            view
                //Case 1: They have labels here (2 of 3 tables),
                //Case 2: The labels are created here too but programatically, according to a web service result (1 of 3 tables)
我使用cellForRowAt函数1)设置标签的文本,或2)以编程方式创建未定义数量的标签,设置它们的文本并使用
cell.view.addSubview(label)
添加它们

tables的框架宽度是205px,但contentSize(我在cellForRowAt函数中设置)通常比这个宽度宽(特别是在标签数量未定义的情况下)。当我水平滚动(垂直滚动到目前为止没有问题)时,问题就出现了,因为在表格contentOffset达到205px的确切时刻,那里的所有标签都消失了。如果我更改表格框架的宽度,标签将在contentOffset与新宽度匹配时消失,这在3个表格中发生。如下所示(表框宽度=205):

contentOffset=204.5

 _________________________
|le2  title3  title4  titl|
|-------------------------|
|el2  label3  label4  labe|
|el2  label3  label4  labe|
|el2  label3  label4  labe|
contentOffset=205

 _________________________
|e2  title3  title4  title|
|-------------------------|
|                         |
|                         |
|                         |
如果你一直滚动,它会一直滚动到最后一个标题的末尾(就像它应该的那样)。标题永远不会消失,只有TableView内容

我已尝试将
\u.frame.size.width
设置为tableViewCell、contentView和view所需的宽度,但没有帮助

代码 考虑到我有其他表,所以一些函数或表验证也用于其他表(每个表都有自己的if或case)。我还更改了表、变量等的名称,因为它们最初与西班牙语非常不同,但我希望它们可以理解

这将是其中一个具有定义数量的标签的表所使用的最相关的代码(我不包括选择行时发生的情况等)

表和滚动视图所在的视图控制器的类 出口和变量

viewDidLoad

负荷表

加载数据

行数部分

cellForRowAt

scrollViewDidScroll

为该表的原型单元格(“数据单元格”)初始化 出口和变量

布局子视图

设置宽度

让我知道,如果你需要我张贴任何其他细节,并为任何英语相关的错误抱歉

@IBOutlet var titlesScrollView : UIScrollView!
//this is the View inside of the ScrollView
@IBOutlet var titlesSubView    : UIView!
@IBOutlet var dataTable        : UITableView!
var dataArray                  : [JSON] = []
override func viewDidLoad() {
    titlesScrollView.delegate = self
    dataTable.delegate        = self
    dataTable.dataSource      = self

    titlesScrollView.contentSize = CGSize(width: titlesSubView.frame.width, height: titlesSubView.frame.height)

    loadTables()
}
func loadTables() {
    self.loadData { error in
        //if error == nil continues loading other tables and so
    }
}
func loadData(onCompletion:@escaping (NSError?)->Void) -> Void {
    DispatchQueue.main.async {
        WebServices.sharedInstance.getData(/*parameters*/) { (json, error) in
            if error == nil {
                if let tempArray = json?["items"].array {
                    self.dataArray = tempArray

                    DispatchQueue.main.async {
                        let offset = self.dataTable.contentOffset
                        self.dataTable.reloadData()
                        self.dataTable.contentOffset(offset, animated: false)
                    }

                    onCompletion(nil)
                } else {
                    /*error handling*/
                }
            } else {
                /*error handling*/
            }
        }
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch tableView {
        //Other cases...
        case dataTable: return self.dataArray.count
        default: return 0
    }

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let index = indexPath.row

    if tableView == otherTables {
        ...
    } else if tableView == dataTable {

        let cell      = tableView.dequeueReusableCell(withIdentifier: "cell") as! dataCell

        cell.id          = dataArray[index]["id"].intValue
        cell.label1.text = dataArray[index]["value1"].stringValue
        cell.label2.text = dataArray[index]["value2"].stringValue
        cell.label3.text = dataArray[index]["value3"].stringValue
        cell.label4.text = dataArray[index]["value4"].stringValue

        tableView.contentSize = CGSize(width: titlesSubView.frame.width, height: tableView.rowHeight*CGFloat(dataArray.count))
        cell.setWidth(titlesSubView.frame.width)

        return cell

    } else {
        ...
    }

}
func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let newOffset = scrollView.contentOffset

    switch scrollView.tag {
        //Other cases (for the other ScrollViews and Tables)...
        case titlesScrollView.tag:
            dataTable.setContentOffset(CGPoint(x: newOffset.x,
                                               y: dataTable.contentOffset.y), animated: false)
        case dataTable.tag:
            titlesScrollView.setContentOffset(CGPoint(x: newOffset.x,
                                                      y: titlesScrollView.contentOffset.y), animated: false)
        default:
            ...
    }

}
@IBOutlet var contentViewCell : UIView!
@IBOutlet var viewCell        : UIView!
@IBOutlet var label1          : UILabel!
@IBOutlet var label2          : UILabel!
@IBOutlet var label3          : UILabel!
@IBOutlet var label4          : UILabel!
var id                        : Int!
private var cellWidth         : CGFloat = 0
override func layoutSubviews() {
    super.layoutSubviews()
    self.frame.size.width = cellWidth
    //Here is where I did set the frame.size.width for the contentView and View too, but it did nothing about my problem
}
func setWidth(_ width: CGFloat){
    cellWidth = width
}