UITableView具有不同的可选部分? [英] UITableView with different optional sections?

查看:42
本文介绍了UITableView具有不同的可选部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a "good" way to solve some special requirements:

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

I have an UITableView with different sections, for example:

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

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

Base Data contains always values (but there is still an variable row count) - and all other "Categories" could contain rows, or still could be empty. If there is no data, the category should be not shown.

我要解决的第一个想法是

No my first idea to solve that is:

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

Create all possible categories (but that could be 20 or more) - and do something like that:

  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,则不返回该部分的标题.

And ill check also with: titleForHeaderInSection if the count is null, and return then no header for the section.

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

BUT: is that a good way? My concern is about creating 20 sections and just 2 are used. Is there another, better way? Can i create sections manually? If yes, should i? So that only the base category is visible, and append everything else if there is data available.

推荐答案

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

This looks like my way of approaching such problems. I'm using enums (Obj-C & especially Swift) to handle and identify my Sections and I always return the full amount of potential sections:

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

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

In func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int, however, I switch the unused sections off by returning 0 rows.

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

The benefit I saw after struggling with your type of dynamic tables was that all sections are always at the same index which made cell management relatively easy:

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

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

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

The same goes for section headers/footers:

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 ;)
    }

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

And the number of rows is calculated/fetched at runtime:

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
    }
}

这篇关于UITableView具有不同的可选部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆