ios - 自定义 uitableViewCell

标签 ios arrays uitableview swift3 cell

我是 iOS 开发的新手,我正在尝试创建自定义 tableViewCells,但我遇到了问题。我有 3 个部分用于我的 tableViewCell 和一个数组。当我尝试为 UITableView, cellForRowAt 中的单元格分配值时,它从每个部分的顶部开始数组。 这是我的代码:

import Foundation
import UIKit

struct cellData {

let cell : Int!
let text : String!
}

class SettingsTableViewCell : UITableViewController{

 var arrayOfCellData = [cellData]()

override func viewDidLoad() {

    arrayOfCellData = [cellData(cell : 1, text : "تغییر رنگ دکمه تنظیمات"),
                       cellData(cell : 2, text : "تغییر تصویر پشت زمینه"),
                       cellData(cell : 1, text : "تغییر رنگ قلم"),
                       cellData(cell : 2, text : "اعلانات"),
                       cellData(cell : 2, text : "صداها"),
                       cellData(cell : 2, text : "زبان"),
                       cellData(cell : 2, text : "بازگشت به حالت پیش فرض"),
                       cellData(cell : 2, text : "سوالات متداول")]


}

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

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

    if section == 0{
        return 3
    }
    else if section == 1{
         return 4
    }
    else if section == 2{

        return 1
    }


    return arrayOfCellData.count
}

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

    if arrayOfCellData[indexPath.row].cell == 1{


        let cell = Bundle.main.loadNibNamed("TableViewCell_Type1", owner: self, options: nil)?.first as! TableViewCell_Type1

        cell.cellName_1.text = arrayOfCellData[indexPath.row].text

        return cell

    }
    else if arrayOfCellData[indexPath.row].cell == 2{

        let cell = Bundle.main.loadNibNamed("TableViewCell_Type2", owner: self, options: nil)?.first as! TableViewCell_Type2

        cell.cellName_2.text = arrayOfCellData[indexPath.row].text

        return cell


    }
    else{

        let cell = Bundle.main.loadNibNamed("TableViewCell_Type1", owner: self, options: nil)?.first as! TableViewCell_Type1

        cell.cellName_1.text = arrayOfCellData[indexPath.row].text

        return cell


    }



}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {



    if section == 0{
        return "تنظیمات رنگ\n"
    }
    else if section == 1{
        return "تنظیمات سیستم\n"
    }
    else if section == 2{

        return "پشتیبانی\n"
    }

    return ""
  }
}

最佳答案

这是一个更方便的数据源的建议

  • 为单元格类型创建一个枚举

    enum CellType {
        case one, two
    }
    
  • CellData 结构包含部分的标题、单元格类型的数组和文本字符串的数组。

    struct CellData {
        let title : String
        let typeArray : [CellType]
        let textArray : [String]
    }
    
  • 声明数据源数组

    var arrayOfCellData = [CellData]()
    
  • viewDidLoad 中创建 3 个部分。我为 textArray 项目使用西方文本表示歉意,因为我对从右到左的文本行为太困惑了)。

    override func viewDidLoad() {
        super.viewDidLoad()
        let section0 = CellData(title: "تنظیمات رنگ\n", typeArray: [.one, .two, .one], textArray: ["Text 1", "Text 2", "Text 3"])
        let section1 = CellData(title: "تنظیمات سیستم\n", typeArray: [.two, .two, .two, .two], textArray: ["Text 4", "Text 5", "Text 6", "Text 7"])
        let section2 = CellData(title: "پشتیبانی\n", typeArray: [.two], textArray: ["Text 8"])
    
        arrayOfCellData = [section0, section1, section2]
    }
    

现在数据源和委托(delegate)方法更容易填充。
以下代码假定这两个单元格都是在 Interface Builder 中直接设计的,具有名为 TableViewCell_Type1TableViewCell_Type2 的自定义类以及适当的标识符 Type1Type2:

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

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

        let section = arrayOfCellData[section]
        return section.textArray.count
    }


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

        let section = arrayOfCellData[indexPath.section]
        let text = section.textArray[indexPath.row]
        let cellType = section.typeArray[indexPath.row]


        switch cellType {
        case .one:
            let cell = tableView.dequeueReusableCell(withIdentifier: "Type1", for: indexPath) as! TableViewCell_Type1
            cell.cellName_1.text = text
            return cell

        case .two:
            let cell = tableView.dequeueReusableCell(withIdentifier: "Type2", for: indexPath) as! TableViewCell_Type2
            cell.cellName_2.text = text
            return cell

        }
    }


    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        let section = arrayOfCellData[section]
        return section.title
    }

关于ios - 自定义 uitableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867951/

相关文章:

ios - 将 ObjC 文件导入 Swift

iphone - MWPhotoBrowser iOS 7 奇怪的定位

c - WIN32和其他c字符串的区别

php - 来自数组的回显值

ios - 检索 firebase 子项并将它们填充到 UITableView 中

java - 查询 Firebase 的位置范围

ios - MakeSchool MakeNotes swift 教程 |创建全局变量

php - 如何将php数组转换为utf8?

ios - 向下滚动时如何停止刷新tableview?

swift - 如何在页脚的 uitableview 中添加另一个 View 到我的初始 View ?