Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在具有自调整单元格大小的小表格视图中显示设置数量的行_Swift_Uikit_Tableview - Fatal编程技术网

Swift 如何在具有自调整单元格大小的小表格视图中显示设置数量的行

Swift 如何在具有自调整单元格大小的小表格视图中显示设置数量的行,swift,uikit,tableview,Swift,Uikit,Tableview,我有一个自定义的表视图类,它被配置为将表视图高度设置为仅显示3行。这意味着如果有20行,表视图的大小将调整为显示前3行,并允许用户滚动 我的这个代码只有在设置静态行高时才有效 class CannedRepliesTableView: UITableView { /// The max visible rows visible in the autocomplete table before the user has to scroll throught them let ma

我有一个自定义的表视图类,它被配置为将表视图高度设置为仅显示3行。这意味着如果有20行,表视图的大小将调整为显示前3行,并允许用户滚动

我的这个代码只有在设置静态
行高时才有效

class CannedRepliesTableView: UITableView {

    /// The max visible rows visible in the autocomplete table before the user has to scroll throught them
    let maxVisibleRows = 3

    open override var intrinsicContentSize: CGSize {

        let rows = numberOfRows(inSection: 0) < maxVisibleRows ? numberOfRows(inSection: 0) : maxVisibleRows
        return CGSize(width: super.intrinsicContentSize.width, height: (CGFloat(rows) * rowHeight))
    }
}
类CannedRepliesTableView:UITableView{
///在用户必须滚动浏览自动完成表之前,表中可见的最大可见行数
设maxVisibleRows=3
打开覆盖变量intrinsicContentSize:CGSize{
设rows=numberOfRows(第0节)

如果我将
UITableViewAutomaticDimension
设置为
行高
,则表视图的大小未正确调整。有什么解决办法吗?

这将是一种改进您当前拥有的功能的方法。我没有为此计算访问
intrinsicContentSize
的经验,也没有在本地进行测试(语法除外),但如果以前它起作用,也应该如此

基本上,您正在创建一个包含
maxVisibleRows
索引路径数的数组。如果数量较少,则
fetchedIndexCount
可防止索引自动边界崩溃。一旦有了数组,就对每个对应的单元格进行迭代并获取其大小,最后求和

class CannedRepliesTableView: UITableView {

    var focussedSection = 0
    let maxVisibleRows = 3

    open override var intrinsicContentSize: CGSize {
        return CGSize(width: super.intrinsicContentSize.width, height: calculateHeight())
    }

    private func calculateHeight() -> CGFloat {
        guard let indexPaths = startIndexes(firstCount: 3) else {
            // Your table view doesn't have any rows. Feel free to return a non optional and remove this check if confident there's always at least a row
            return 0
        }
        return indexPaths.compactMap({ cellForRow(at: $0)?.intrinsicContentSize.height }).reduce(0, +)
    }

    private func startIndexes(firstCount x: Int) -> [IndexPath]? {
        let rowsCount = numberOfRows(inSection: focussedSection)
        let fetchedIndexesCount = min(x, rowsCount)
        guard fetchedIndexesCount > 0 else {
            return nil
        }
        var result = [IndexPath]()
        for i in 0..<fetchedIndexesCount {
            result.append(IndexPath(row: i, section: focussedSection))
        }
        return result
    }
}
类CannedRepliesTableView:UITableView{
var focussedSection=0
设maxVisibleRows=3
打开覆盖变量intrinsicContentSize:CGSize{
返回CGSize(宽度:super.intrinsicContentSize.width,高度:calculateHeight())
}
private func calculateHeight()->CGFloat{
guard let indexPaths=StartIndex(firstCount:3)else{
//您的表视图没有任何行。如果始终至少有一行,请随意返回非可选项并删除此检查
返回0
}
返回indexPaths.compactMap({cellForRow(at:$0)?.intrinsicContentSize.height})。reduce(0,+)
}
私有func startIndexes(firstCount x:Int)->[IndexPath]{
让rowsCount=numberOfRows(章节:聚焦部分)
设fetchedIndexCount=min(x,行计数)
guard获取的索引计数>0其他{
归零
}
var result=[IndexPath]()

对于0中的i..由于单元格是自调整大小的,您如何知道所有单元格的高度都相等?当滚动可以找到大小不同的单元格时,如何确定每次显示任意3个单元格所需的高度?这就是我的问题:(我想将内部大小设置为前3个单元格的高度。没有解决方案吗?如果没有,我必须将行高设置为固定值,然后:(我尝试过这个方法,虽然它可以获取单元格和其他内容,但它可以从具有动态维度的表视图单元格中获取0高度。无论如何,感谢您的帮助,我将使用静态高度来实现这一点。我想您可以看看用
SystemLayoutSizeFiting(CGSize)替换对intrinsicContentSize.height的检查(宽度:providedWidth,height:0)).height
嗯,事实证明,在我的情况下,使用固定宽度而不是自动调整大小要简单得多。无论如何,感谢您的帮助