ios - 在运行时更改 tableview 高度

标签 ios objective-c iphone swift

你好, 我正在创建一个菜单作为表格 View ,并在单击按钮时切换其可见性。我实际上是在改变它的高度。但是改变 tableview 的高度只影响 tableview 而不是它里面的单元格。 TableView 也有阴影,如果我删除阴影,它会完美地工作。请帮忙。 这是一些代码片段

let _initialTableFrame = CGRectMake(self.view.frame.size.width - 150, 68, 140, 0)
    _topMenuTableView = UITableView(frame: _initialTableFrame)
    _topMenuTableView.delegate = self
    _topMenuTableView.dataSource = self
    _topMenuTableView.rowHeight = 35
    _topMenuTableView.tag = 100
    _topMenuTableView.alpha = 0.9
    _topMenuTableView.tableFooterView = UIView()
    _topMenuTableView.scrollEnabled = false

    _topMenuTableView.layer.cornerRadius = 5
    _topMenuTableView.layer.shadowColor = UIColor.blackColor().CGColor
    _topMenuTableView.layer.shadowOffset = CGSizeMake(0, 0)
    _topMenuTableView.layer.shadowRadius = 5.0
    _topMenuTableView.layer.shadowOpacity = 1 
func toogleTopMenu()
{
    if _isTopMenuVisible
    {
        let _currentTopMenuFrame = _topMenuTableView.frame
        let _tempFrame = CGRectMake(_currentTopMenuFrame.origin.x, _currentTopMenuFrame.origin.y, _currentTopMenuFrame.size.width, 0)


        UIView.animateWithDuration(0.2, animations: {self._topMenuTableView.frame = _tempFrame})
        _isTopMenuVisible = false
    }
    else
    {
        let _currentTopMenuFrame = _topMenuTableView.frame
        let _tempFrame = CGRectMake(_currentTopMenuFrame.origin.x, _currentTopMenuFrame.origin.y, _currentTopMenuFrame.size.width, 38)

        UIView.animateWithDuration(0.2, animations: {self._topMenuTableView.frame = _tempFrame})
        _isTopMenuVisible = true
    }
}

最佳答案

这是因为您只是更改了 tableView 的框架,而不是 tableViewCellHeight

试试这个

    func toogleTopMenu()
{
    if _isTopMenuVisible
    {
        let _currentTopMenuFrame = _topMenuTableView.frame
        let _tempFrame = CGRectMake(_currentTopMenuFrame.origin.x, _currentTopMenuFrame.origin.y, _currentTopMenuFrame.size.width, 0)

        UIView.animateWithDuration(0.2, animations: {self._topMenuTableView.frame = _tempFrame
_topMenuTableView.rowHeight = //whatever Height you want to assign
_topMenuTableView.reloadDate()
})

        _isTopMenuVisible = false
    }
    else
    {
        let _currentTopMenuFrame = _topMenuTableView.frame
        let _tempFrame = CGRectMake(_currentTopMenuFrame.origin.x, _currentTopMenuFrame.origin.y, _currentTopMenuFrame.size.width, 38)
    UIView.animateWithDuration(0.2, animations: {self._topMenuTableView.frame = _tempFrame
_topMenuTableView.rowHeight = //whatever Height you want to assign
_topMenuTableView.reloadDate()
})
    _isTopMenuVisible = true
}
}

这会有所帮助。谢谢

关于ios - 在运行时更改 tableview 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140648/

相关文章:

ios - flutter:如果 app delegate swizzling 被禁用,UIApplicationDelegate 收到的远程通知需要转发到 FIRAuth 的

ios - UITabBarController 图像未显示

iphone - 有没有办法获取 NSUserDefaults 中的所有值?

ios - 提交同名的 iPhone 和 iPad 版应用

ios - 是否可以为数字键盘上的删除键添加目标方法?

ios - 如何从包含的应用程序更新我的 iOS Today 小部件?

ios - UISegmentedControl 选中为空

ios - 禁用 SWRevealViewController 在 addGestureRecognizer 上弹跳

iphone - 如何打印存储在 iphone 上的所有 sqlite 数据库

iphone - 如何将多个目录中的一组视频打包并在iPhone应用程序中播放?