ios - swift 用 2 个数组填充 UItableViewCell

标签 ios swift uitableview

我有一个带有 2 个 UITableViewCell 的基本 uitableview 我试图用 array1.count 填充第一个单元格,用 array2.count 填充第二个单元格,但我不知道如何,这是我的代码:

override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var value: Int = 0
        if(section == 0){
            value = 1
        }else if(section == 2){
            value = 5
        }
        return value
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell1, for: indexPath) as!  Cell1
            cell.lbl_cell1.text = "Subject"
            cell.selectionStyle = .none
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell2, for: indexPath) as!  Cell2
            cell.lbl_cell2.text = "Lorem Ipsum es simplemente el texto de relleno de las imprentas y archivos de texto. Lorem Ipsum ha sido el texto de relleno estándar de las industrias desde el año 1500, cuando un impresor (N. del T. persona que se dedica a la imprenta) desconocido usó una galería de textos y los mezcló de tal manera que logró hacer un libro de textos especimen."
            cell.selectionStyle = .none
            return cell
        }
        return UITableViewCell()
    }

我的错误是我总是在每个单元格中得到 1 个值,我想要的就是这个

单元格 1 数学 单元格 2 a,b,c,d,e,f,g,h .....

提前致谢

最佳答案

试试这个:

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var value: Int = 0
    if(section == 0){
        value = array1.count
    }else{
        value = array2.count
    }
    return value
 }

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell1, for: indexPath) as!  Cell1
        item = array1[indexPath.row]
        ...
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell2, for: indexPath) as!  Cell2
        item = array2[indexPath.row]
        ...
        return cell
    }
}

关于ios - swift 用 2 个数组填充 UItableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53861929/

相关文章:

objective-c - 在一行UITable中设置多个UILabel

ios - 类型 * 的值没有成员 'rx'

ios - 具有许可证 key 激活的 Iphone 应用程序

ios - 无法让 UIScrollView 在 UITableViewCell 内部被识别,nil 错误

ios - 如何让两个不同的表格 View 单元格始终成对显示在同一个表格 View 中?

swift - 实现 "floating bubble"对象的最佳方法

iphone - iOS 密码强度检查器

ios - Int64 不工作,而 Int 工作

swift - 为什么 UIStackView 调整排列 subview 的大小?

ios - 我如何转换为特定时区?