将索引列表和节标题添加到已翻译的 tableview [英] Adding index list and section headers to translated tableview

查看:22
本文介绍了将索引列表和节标题添加到已翻译的 tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在这个用例中将部分标题和索引列表添加到UITableView?

@IBOutlet var tableView:UITableView!var 详细信息:详细信息?= 零var list = [表数据]()让搜索 = UISearchController(searchResultsController: nil)覆盖 func viewDidLoad() {super.viewDidLoad()列表 = [tabledata(name:"something".localized, sort:"sort.something".localized, id:"something.html"),tabledata(name:"somethingelse".localized, sort:"sort.somethingelse".localized, id:"somethingelse.html"),...]func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "library", for: indexPath)var 数据:表数据数据 = 列表[indexPath.row]cell.textLabel!.text = data.name返回单元格}

现在的重点是要翻译表格数据.

<块引用>

如果用户将语言设置为英语,则不需要排序;
但如果语言设置为德语等,则必须将部分标题应用于翻译表.

谁能弄清楚如何从字典内部调用一个section letter,比如

tabledata(section: "a", name:"anaconda".localized, sort:"sort.anaconda".localized, id:"anaconda.html")

注意

  • name: 是将要.localized
  • 的实际单元格名称
  • sort: 必须帮助像 á é 等字符在单元格名称中正确排序(避免它们显示在最后字母)
  • id: 调用要在 detailViewController 中显示的 html 文件位置(因为名称必须被翻译,我们需要一个静态文本)

节标题和索引列表的通常实现将导致类似

T//节头翻译//单元格名称传播...T//表中Übersetzung//另一种语言格特里贝...

<块引用>

也许可以有一个 function() 它将从 tabledata 获取所有 section 并分配将它们转换为 var sectionletters = ["A", "B", ...] 中的相应字母,但这超出了我对初学者的有限知识.

翻译 tableview 的正确模型是什么?

感谢您的帮助!

解决方案

有几种方法可以实现这一点.最简单的方法之一是使用 UITableView 提供的委托函数.

出于组织目的,我首先创建一个类来表示部分.像这样:

class GhostSection {//此部分的标题var sectionTitle: 字符串//此特定部分的项目列表var 项目:[Ghost] = []init(title: String, items: [Ghost]) {部分标题 = 标题self.items = 物品}}

然后更新 list 变量以使用部分而不是原始项目列表:

var tabledata = [GhostSections]()

理想情况下,我会从一个 json 对象创建我的 GhostSection,但如果必须手动完成,那么这样的事情会起作用:

//设置表数据以对每个部分使用 GhostSection 实例var 表数据 = [GhostSection(title: "a", items: [Ghost(name:"an".localized, sort:"sort.an".localized, id:"0101")]),GhostSection(title: "b", items: [Ghost(name:"bc".localized, sort:"sort.bc".localized, id:"0102")]),GhostSection(title: "c", items: [Ghost(name:"cd".localized, sort:"sort.cd".localized, id:"0103")])]

我会让 numberOfSections(in tableView: UITableView) 返回 tabledata 计数,这将是部分总数的计数:

func numberOfSections(in tableView: UITableView) ->整数{返回 tabledata.count}

numberOfRowsInSection 返回 tabledata 数组中每个 GhostSection 上每个 items 数组的列表计数:

func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回 tabledata[section].items.count}

然后我会更新 cellForRow(at indexPath) 以返回该部分中正确单元格的信息:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "library", for: indexPath)//这将获取部分数据,然后从该部分获取项目var data = list[indexPath.section].items[indexPath.row]cell.textLabel?.text = data.name返回单元格}

我也喜欢将标题创建为可重复使用的笔尖视图,因此 ListHeaderView 中只有一个标题标签:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) ->界面视图?{//这从笔尖加载视图守卫让视图 = ListHeaderView.fromNib() as?ListHeaderView 其他 {返回零}//获取当前节的节对象让数据 = 表数据[节]//并从我们的部分对象在此处设置标题标签文本view.titleLabel.text = data.sectionTitle返回视图}//如果你还想有一个自定义页脚,你可以在这里做...func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) ->界面视图?{返回 UIView()}//并让标题高度是动态的,或者在这里返回一个设置的高度...func tableView(_ tableView: UITableView, heightForHeaderInSection 部分: Int) ->CGFloat {返回 UITableViewAutomaticDimension}

如果您想使用这种方法,这里是我实现 fromNib() 函数的方法,以便您可以实例化任何具有关联 .xib 文件的视图:

公共扩展 UIView {公共静态函数 fromNib() ->界面视图?{让nibName = String(描述:自我)让捆绑=捆绑(对于:自我)让 nib = UINib(nibName: nibName, bundle: bundle)守卫让视图 = nib.instantiate(withOwner: nil, options: nil).first as?UIView 其他 {打印(无法实例化笔尖命名:\(nibName)在包中:\(字符串(描述:包))-笔尖:\(字符串(描述:笔尖))")返回零}返回视图}}

另请注意,如果您想保持当前的惯例,那么养成使用驼峰命名的习惯,其中第一个字母大写的类/对象名称是一个好主意.因此,不要将您的类命名为 ghost,而是养成使用 Ghost 的习惯.对于变量,约定也是驼峰式大小写,但以小写开头.

How to add section headers and index list to UITableView in this use case?

@IBOutlet var tableView: UITableView!

var detail: Detail? = nil
var list = [tabledata]()
let search = UISearchController(searchResultsController: nil)

override func viewDidLoad() {

    super.viewDidLoad()
    list = [

        tabledata(name:"something".localized, sort:"sort.something".localized, id:"something.html"),
        tabledata(name:"somethingelse".localized, sort:"sort.somethingelse".localized, id:"somethingelse.html"),
        ...

    ]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "library", for: indexPath)
    var data: tabledata
    data = list[indexPath.row]
    cell.textLabel!.text = data.name
    return cell

}

Now the point is that table data are going to be translated.

Like if the user has set the language to English, sorting is unneeded;
but if the language is set to German etc., section headers have to be applied to translated table.

Can someone figure out how to call a section letter from inside the dictionary, like

tabledata(section: "a", name:"anaconda".localized, sort:"sort.anaconda".localized, id:"anaconda.html")

Note that

  • name: is the actual cell name going to be .localized
  • sort: has to help characters like á é etc. in cell name to sort properly (avoiding them to show in the end of alphabet)
  • id: calls the html file location to display in detailViewController ('cause name has to be translated and we want a static text here)

A usual implementation of section headers and index list will result in something like

T                 // section header
translation       // cell names
transmission
...

T                 // table in
Übersetzung       // another language
Getriebe
...

Maybe there could be a function() which will get all the sections from tabledata and assign them to corresponding letters in var sectionletters = ["A", "B", ...] but that's above my beginner-limited knowledge.

What's the correct model for translated tableview?

Thanks for help!

解决方案

There are a few ways to accomplish this. One of the simplest is to use the delegate functions provided with UITableView.

For organizational purposes I'd start by creating a class to represent sections. Something like this:

class GhostSection {

    // a title for this section
    var sectionTitle: String

    // a list of items for this particular section
    var items: [Ghost] = []

    init(title: String, items: [Ghost]) {
        sectionTitle = title
        self.items = items
    }
}

Then update the list variable to use the sections instead of a raw list of items:

var tabledata = [GhostSections]()

Ideally I'd create my GhostSection from a json object, but if it must be done manually then something like this would work:

// setup the table data to use GhostSection instances for each section
var tabledata = [
    GhostSection(title: "a", items: [Ghost(name:"an".localized, sort:"sort.an".localized, id:"0101")]),
    GhostSection(title: "b", items: [Ghost(name:"bc".localized, sort:"sort.bc".localized, id:"0102")]),
    GhostSection(title: "c", items: [Ghost(name:"cd".localized, sort:"sort.cd".localized, id:"0103")])
] 

I'd have numberOfSections(in tableView: UITableView) return the tabledata count, which would be the count of the total number of sections:

func numberOfSections(in tableView: UITableView) -> Int {
    return tabledata.count
}

and the numberOfRowsInSection return the list count for each items array on each GhostSection in your tabledata array:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tabledata[section].items.count
}

Then I'd update cellForRow(at indexPath) to return the info for the right cell in that section:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "library", for: indexPath)
    // this gets the section data, then gets the item from that section
    var data = list[indexPath.section].items[indexPath.row]
    cell.textLabel?.text = data.name
    return cell

}

I also like to create my headers as reusable nib views, so the ListHeaderView just has a title label in it:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    // this loads the view from the nib
    guard let view = ListHeaderView.fromNib() as? ListHeaderView else {
        return nil
    }

    // get the section object for this current section
    let data = tabledata[section]

    // and set the title label text here from our section object
    view.titleLabel.text = data.sectionTitle
    return view
}

// if you want to also have a custom footer you can do that here...
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView()
}

// and let the header height be dynamic, or return a set height here...
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}

In case you want to use this approach here's how I implemented the fromNib() function so you can instantiate any view that has an associated .xib file:

public extension UIView {

    public static func fromNib() -> UIView? {
        let nibName = String(describing: self)
        let bundle = Bundle(for: self)
        let nib = UINib(nibName: nibName, bundle: bundle)
        guard let view = nib.instantiate(withOwner: nil, options: nil).first as? UIView else {
            print("Unable to instantiate nib named: \(nibName) in bundle: \(String(describing: bundle)) -- nib: \(String(describing: nib))")
            return nil
        }
        return view
    }
}

Also note it's a good idea to get in the habit of using camel case for class/object names with the first letter capitalized IF you want to keep with current conventions. So instead of naming your class ghost get in the habit of doing Ghost. For variables the convention is also camel case but start with a lowercase.

这篇关于将索引列表和节标题添加到已翻译的 tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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