动态调整UITableView的大小,该大小将显示为不可滚动的内容列表

时间:2017-04-05 10:28:06

标签: ios swift uitableview swift3 uiscrollview

我正在使用Swift 3.0中的一个项目,它在UIScrollView中有一个UITableView,它已经设置了它的约束。

要求是显示从这个UITableView中的数组返回的歌曲列表,但是这个UITableView的大小应该根据数组的大小动态排列,所以我不需要滚动UITableView,因为我可以看到UITableView中的所有歌曲(这个UITableView在我的scrollView中,所以我可以使用我的scrollView在列表中向上或向下移动)。

我将如何实现这一目标?

2 个答案:

答案 0 :(得分:4)

您可以根据内容更改tableview的高度。

point = Literal(".")
e = CaselessLiteral("e")
fnumber = Combine(Optional("-") + Word(nums) +
                  Optional(point + Optional(Word(nums))) +
                  Optional(e + Word("+-" + nums, nums))).setParseAction(self.pushfirstfunc("number"))
ident = Word(alphas, alphas + nums + "_")
funcname = Word(alphas, alphas + nums + "_")

plus = Literal("+")
minus = Literal("-")
uminus = Literal("-")
mult = Literal("*")
div = Literal("/")
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
lparfunc = Literal("(").suppress()
rparfunc = Literal(")").suppress()
comma = Literal(",")
addop = plus | minus
multop = mult | div
expop = Literal("^")

self.expr = Forward()
atom_paren = (lpar + self.expr + rpar)
atom_ident = ident.setParseAction(self.pushfirstfunc("identifier"))
atom_func = (funcname + lparfunc + self.expr + ZeroOrMore(
        (comma + self.expr).setParseAction(self.pushfirstfunc("argument"))) + rparfunc).setParseAction(
        self.pushfirstfunc("function"))
atom = (fnumber |
        atom_func | (uminus + atom_func).setParseAction(self.pushfirstfunc("unary-")) |
        atom_ident | (uminus + atom_ident).setParseAction(self.pushfirstfunc("unary-")) |
        atom_paren | (uminus + atom_paren).setParseAction(self.pushfirstfunc("unary-"))
       )

factor = Forward()
factor << atom + ZeroOrMore((expop + factor).setParseAction(self.pushfirstfunc("infix")))
term = factor + ZeroOrMore((multop + factor).setParseAction(self.pushfirstfunc("infix")))
self.expr << term + ZeroOrMore((addop + term).setParseAction(self.pushfirstfunc("infix")))
self.bnf = self.expr + stringEnd
self.bnf.ignore(cStyleComment)

如果您正在为tableview高度使用autolayout create outlet并将tableview内容大小分配给其常量。

var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame

答案 1 :(得分:0)

1。为tableView创建一个子类并重写internalContentSize。

         class MyOwnTableView: UITableView {
        override var intrinsicContentSize: CGSize {
            self.layoutIfNeeded()
            return self.contentSize
        }

        override var contentSize: CGSize {
            didSet{
                self.invalidateIntrinsicContentSize()
            }
        }

        override func reloadData() {
            super.reloadData()
            self.invalidateIntrinsicContentSize()
        }
    }

2。在“界面”构建器中,将tableView的类更改为MyOwnTableView(UITableView的子类)。

  1. 设置表格视图的自动行高。

        tableView.estimatedRowHeight = 60.0
        tableView.rowHeight = UITableViewAutomaticDimension