简体   繁体   English

自定义tableViewCell

[英]Custom tableViewCell

I'm new to iOS development and I'm trying to create custom tableViewCells but I've got a problem. 我是iOS开发的新手,正在尝试创建自定义tableViewCells,但是遇到了问题。 I have 3 sections for my tableViewCell and an Array.When I try to assign values to cells at UITableView, cellForRowAt it start the array from the top for each section. 我的tableViewCell和一个数组有3个部分,当我尝试为UITableView, cellForRowAt单元格分配值时UITableView, cellForRowAt会从每个部分的顶部开始数组。 Here's my code: 这是我的代码:

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

This is a suggestion with a more convenient data source 这是一个建议,建议使用更方便的数据源

  • Create an enum for the cell types 为单元格类型创建一个枚举

     enum CellType { case one, two } 
  • The CellData struct contains the title for the sections, an array for the cell types and an array for the text strings. CellData结构包含这些部分的标题,单元格类型的数组和文本字符串的数组。

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

     var arrayOfCellData = [CellData]() 
  • In viewDidLoad create the 3 sections. viewDidLoad创建3个部分。 I apologize for using western text for the textArray items, because I got too confused about the right to left text behavior). 我为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] } 

Now the data source and delegate methods are much easier to populate. 现在,数据源和委托方法更容易填充。
The following code assumes that both cells are designed directly in Interface Builder with custom classes named TableViewCell_Type1 and TableViewCell_Type2 and appropriate identifiers Type1 and Type2 : 以下代码假定两个单元都是直接在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
    }

Instead of arrayOfCellData[indexPath.row], try this to calculate correct index in array: 代替arrayOfCellData [indexPath.row],请尝试以下操作以计算数组中的正确索引:

   var idx = indexPath.row
   if indexPath.section == 1 { idx += 3 }
   else if indexPath.section == 2 { idx += 7 }

   // and then you can use:
   arrayOfCellData[idx]

I changed my code this way, and it worked! 我以这种方式更改了代码,并且成功了!

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

    var offset = 0

    if indexPath.section == 0 {

        offset = 0

    } else if indexPath.section == 1 {

        offset = 3

    } else if indexPath.section == 2 {

        offset = 7

    }


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


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

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

        return cell

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

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

        cell.cellName_2.text = arrayOfCellData[indexPath.row + offset].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 + offset].text

        return cell

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM