Ios 在UITableViewCell中设置UIScrollview的内容
我有一个tableview,我想在其中的行中显示uiscroll视图。我已将tableview的高度设置如下:Ios 在UITableViewCell中设置UIScrollview的内容,ios,iphone,swift,uitableview,autolayout,Ios,Iphone,Swift,Uitableview,Autolayout,我有一个tableview,我想在其中的行中显示uiscroll视图。我已将tableview的高度设置如下: func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ let deviceFrame = UIScreen.main.bounds.size print((1.21*deviceFrame.width)) return (1.21
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
let deviceFrame = UIScreen.main.bounds.size
print((1.21*deviceFrame.width))
return (1.21*deviceFrame.width)
}
func loadContent(){
var xaxis = 0
var numOfPages = 0
let screenSize = UIScreen.main.bounds.size
for index in 1...3 {
let pagevw = CustomPageView(frame: CGRect(x: xaxis, y: 0, width: Int(scrlvw.frame.size.width), height: Int(scrlvw.frame.size.height-10)))
pagevw.translatesAutoresizingMaskIntoConstraints = true
scrlvw.addSubview(pagevw)
xaxis = xaxis + Int(screenSize.width)
numOfPages += 1
}
let content = CGSize(width: numOfPages * (Int(scrlvw.frame.height)), height: Int(scrlvw.frame.height-30))
print("content")
print(content)
scrlvw.contentSize = CGSize(width: numOfPages * (Int(scrlvw.frame.height)), height: Int(scrlvw.frame.height-30))
pagecontrol.numberOfPages = numOfPages
}
现在,我有了一个自定义表视图单元格,在该表视图单元格中,我尝试为uiscrollview添加页面,如下所示:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
let deviceFrame = UIScreen.main.bounds.size
print((1.21*deviceFrame.width))
return (1.21*deviceFrame.width)
}
func loadContent(){
var xaxis = 0
var numOfPages = 0
let screenSize = UIScreen.main.bounds.size
for index in 1...3 {
let pagevw = CustomPageView(frame: CGRect(x: xaxis, y: 0, width: Int(scrlvw.frame.size.width), height: Int(scrlvw.frame.size.height-10)))
pagevw.translatesAutoresizingMaskIntoConstraints = true
scrlvw.addSubview(pagevw)
xaxis = xaxis + Int(screenSize.width)
numOfPages += 1
}
let content = CGSize(width: numOfPages * (Int(scrlvw.frame.height)), height: Int(scrlvw.frame.height-30))
print("content")
print(content)
scrlvw.contentSize = CGSize(width: numOfPages * (Int(scrlvw.frame.height)), height: Int(scrlvw.frame.height-30))
pagecontrol.numberOfPages = numOfPages
}
现在,虽然单元格显示正确,但表的内容大小并没有正确更新。可能自动布局在这里造成了一些麻烦。尽管我希望autolayout保持不变,因为我的页面中有许多组件。当您向UIScrollView动态添加自动布局内容时,您希望向子视图添加适当的约束,而不是使用设置每个子视图的x、y、w、h然后计算scrollview的ContentSize的旧样式 这样做时,您不仅可以使用约束来定位“页面”(嵌入的子视图),还可以“挤出”scrollview内容大小的侧面 我制作了一个向嵌入在垂直滚动UIScrollView中的水平滚动UIScrollView添加子视图的示例。它在Swift 2.2中,但它会自动更新到3.0而不会出现问题 我使用了几种不同的方法来分配约束,所有这些方法都完成了相同的任务。“工作”方法仍在主视图控制器类中
您应该能够复制这些技术来解决您的滚动需求。您在这里发布的任何内容都没有显示与“表的内容大小”相关的内容。。。您正在返回
(1.21*deviceFrame.width)
作为表中每行的高度——您的意思是在表中创建12行这样的行,但表不会上下滚动吗?我的意思是表行中UIScrollView的内容大小,表很好,但scrollview由于自动布局而无法获得正确的内容大小。