簡體   English   中英

自定義tableViewCell

[英]Custom tableViewCell

我是iOS開發的新手,正在嘗試創建自定義tableViewCells,但是遇到了問題。 我的tableViewCell和一個數組有3個部分,當我嘗試為UITableView, cellForRowAt單元格分配值時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] } 

現在,數據源和委托方法更容易填充。
以下代碼假定兩個單元都是直接在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
    }

代替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]

我以這種方式更改了代碼,並且成功了!

 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