2016-11-21 60 views
0

我对工作如下: Screenshot点击时。调整的tableView全屏

基本上它是一个UIImageView的另一个的UIImageView以内的顶部(在屏幕的60%),而这需要的40%的UITableView屏幕

和代码(见下文):

我想实现是调整时,我customCell被点击的UITableView的全屏的细胞和unsize它是未点击时的原始状态。

有什么办法可以使这项工作? 在此先感谢。

//Label outlets 

@IBOutlet weak var tableView: UITableView! 


var selectedIndexPath : IndexPath? = nil 


override func viewDidLoad() { 
    super.viewDidLoad() 

//MARK Start of CustomViewCell Code 

    tableView.register(UINib(nibName: "CustomViewCell", bundle: nil), forCellReuseIdentifier: "Cell") 

} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 

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

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

    cell.clipsToBounds = true 

    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    let index = indexPath 

    if selectedIndexPath != nil { 
     if index == selectedIndexPath { 
      return 667 
     } else { 
      return 62 
     } 

    } else { 

     return 62} 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    switch selectedIndexPath { 
    case nil: 
     selectedIndexPath = indexPath 
    default: 
     if selectedIndexPath! == indexPath { 
      selectedIndexPath = nil 
     } else { 
      selectedIndexPath = indexPath 
     } 
    } 

    tableView.reloadRows(at: [indexPath], with: .automatic) 

} 

    //MARK End of CustomViewCell Code 
+0

什么是你'tableViewCell'的,你会全屏显示的内容?它有一个'imageView'? – Santosh

+0

没有@Santosh'标签'和'textView' – j0h4nf

回答

1

除了重新加载具有新高度的单元格之外,还需要更改表格视图的框架。如果你想使表占据了整个屏幕,请确保您为它创建一个IBOutlet,说myTableView,那么,后:

tableView.reloadRows(at: [indexPath], with: .automatic) 

添加以下代码:

myTableView.frame = view.bounds 

您还可以创建一个Bool变量来跟踪表视图的正常和全屏模式,以及一些CGRect变量来保持初始帧。当你想让表格视图返回到其初始框架时,你可能需要这样的东西:

myTableView.frame = initialTableViewFrame 

希望你明白了。

+0

你已经失去了我@ppalancia 我得到调整'tableView'的想法,我已经宣布为一个IBOutlet,但那么我怎么让这个'tableView'成为一个布尔变量。 我已经尝试了一些东西,但不能让它工作。如果你知道一个教程,让我度过那个很棒的教程。请不要告诉我完整的解决方案,因为我想尽我所能了解自己。 在此先感谢。 – j0h4nf

+0

@ j0h4nf ...我的意思是你的视图控制器中的表格视图属于Bool变量/属性。看看我是否有时间为你写一篇教程。 – ppalancica

0

这是一个纯粹的自动布局方法:

1.In你的界面生成器,创建具有约束[追踪,领导,顶部,底部,高度]一个UIImageView,使高度> =和优先级为999 。稍后将在代码中使用此高度来展开/折叠逻辑。 Elements Layouts

2.创建一个虚拟的UIView,其高度将在稍后进行编程设置,这将UIView的“推” /“拉伸”你的身高在创建扩大影响到您的ImageView。将它设置为隐藏可见性,因为您不需要将其显示给用户。 Elemets Layouts

3.In您的自定义单元格,创建两个NSLayoutConstraint实例作为@IBoutlets,一个是身高的UIImageView和一个用于虚拟UIView的高度,然后在你的接口文件之后将它们连接起来。

let lowPriorityHeight : Float = 250; 
let highPriorityHeight : Float = 999; 
class CustomCell: UITableViewCell { 
    @IBOutlet weak var imgVwHeight: NSLayoutConstraint! 
    @IBOutlet weak var tempHeight : NSLayoutConstraint! 
    var isExpanded = false{ 
     didSet{ 
      imgVwHeight.priority = (isExpanded) ? lowPriorityHeight : highPriorityHeight; 
      if isExpanded == true{ 
       tempHeight.constant = ExpandedHeight 
      }else{ 
       tempHeight.constant = NormalHeight 
      } 
     } 
    } 

4.In主类,设置isExpanded为真或假,以创建效果。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let cell = tableView.cellForRow(at: indexPath) as! CustomCell 
     cell.isExpanded = !cell.isExpanded 
     tableView.reloadData() 
    } 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "kCustomCell", for: indexPath) as! CustomCell 
     return cell 
    } 

您可以下载我的示例实现HERE

+0

感谢@janusfidel,但这不是我正在寻找的解决方案。我真的很想找到一种方法让我的工作方式开始。 – j0h4nf