繁体   English   中英

UITableView具有不同的可选部分?

[英]UITableView with different optional sections?

我正在寻找一种解决某些特殊要求的“好方法”:

我有一个带有不同部分的UITableView,例如:

  • 基础数据
  • 关于我
  • 兴趣
  • 图片

基本数据始终包含值(但行数仍然可变)-所有其他“类别”可能包含行,或者仍然为空。 如果没有数据,则不显示类别。

我要解决的第一个想法是:

创建所有可能的类别(但可以是20个或更多)-并执行以下操作:

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        var count:Int = 0

        switch (section) {
        case 0:
            count = baseHeaders.count
        case 1:
            if(mapItem.courses?.count > 0) {
                count = mapItem.courses!.count
            }
            break;
        default:
            count = 0
        }

        return count
    }

还要使用以下命令检查: titleForHeaderInSection如果计数为null,则不返回该部分的标题。

但是:那是个好方法吗? 我关心的是创建20个部分,仅使用2个部分。 还有另一种更好的方法吗? 我可以手动创建部分吗? 如果是,我应该吗? 这样,只有基本类别可见,如果有可用数据,则附加所有其他内容。

这看起来像是我解决此类问题的方式。 我使用枚举(Obj-C,尤其是Swift)来处理和识别我的版块,并且我总是返回全部潜在的版块:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return FormSection.count // enum function
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int中,我通过返回0行来关闭未使用的部分。

在为您的动态表类型苦苦挣扎之后,我看到的好处是所有部分始终处于同一索引,这使得单元格管理相对容易:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let section:FormSection = FormSection(rawValue:indexPath.section)!

    switch section {
    case .Header:
        //…
    default:
        //…
    }
}

节的页眉/页脚也是如此:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    switch section {
    case FormSection.Header.rawValue:
        return nil
    case FormSection.RoomSetup.rawValue where foo == false:
        return nil
    default:
        // return header with title = FormSection(rawValue: section)?.headerTitle()
        // Swift enums ftw ;)
    }

行数是在运行时计算/获取的:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let section:FormSection = FormSection(rawValue:section)!
    switch section {
    case .Section1:
        return fooExpanded ? (numberOfFoo) : 0
    case .Section2:
        return model.barCount()
    default:
        return 1
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM