我如何使用swift使用两个不同的数组填充表格视图中的两个部分?

问题描述 投票:29回答:4

我有两个数组Data1和Data2,我想将每个数组中的数据(它们包含字符串)填充到两个不同部分的表视图中。第一部分的标题为“ Some Data 1”,第二部分的标题为“ KickAss”。

我都有两个部分都填充了第一个数组中的数据(但也没有标题)。>>

到目前为止是我的代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rowCount = 0
        if section == 0 {
            rowCount = Data1.count
        }
        if section == 1 {
            rowCount = Data2.count
        }
        return rowCount
    }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        let ip = indexPath
        cell.textLabel?.text = Data1[ip.row] as String
        return cell
    }

在cellForRowAtIndexPath方法中,像我在numberOfRowsInSection方法中所做的一样,是否可以标识该节?另外,如何给每个部分标题?谢谢

我有两个数组Data1和Data2,我想将每个数组中的数据(它们包含字符串)填充到两个不同部分的表视图中。第一部分应具有标题“ Some ...

arrays uitableview swift sections
4个回答
67
投票

TableView单元格


23
投票

您可以创建一个Struct来保存属于某个部分的数据,以替代我以前的回答。例如:


14
投票

您可以通过查看indexPath.section来确定您所在的部分。要指定标题,请覆盖功能


0
投票

在swit 4或swift 5中,您可以使用like。这里显示了自定义标头部分wit过滤器

© www.soinside.com 2019 - 2024. All rights reserved.