2016-11-29 104 views
0

我是新来iOS开发,我想创建自定义tableViewCells,但我有一个问题。我有3个部分为我的tableViewCell和一个数组。当我尝试将值分配给UITableView, cellForRowAt的单元格时,它从每个部分的顶部开始数组。 这里是我的代码:自定义tableViewCell

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

不要使用像“Bundle.main.loadNibNamed”这样的旧编码方式,而应使用“registerNib”来帮助您创建单元格。另外,你可以发布你的问题的屏幕截图吗? – Satyam

+0

在'cellForRowAtIndexPath'中,您只考虑索引路径的行。因此,每个部分显示数组顶部的相同对象。此外,你可以在Interface Builder中设计多个自定义单元格(我猜是自Xcode 6以来),不需要额外的笔尖。最后不要硬编码'numberOfRows'。用嵌套数组或其他东西创建一个合适的模型,以便始终使用'count'属性。 – vadian

+0

您不能只考虑'indexPath.row',根据'indexPath.section'和'indexPath.row'计算数组索引。然后你会从数组中得到确切的元素。 –

回答

0

我改变了我的代码,这样一来,它的工作!

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 

     } 
0

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

使用你的解决方案后,我有一个编译器错误。它抱怨这个错误:二元运算符'=='不能应用于类型'section.Type'和'Int'的操作数 – fatemeh

+0

@fatemeh当你想使用'=='时,你应该在2个值上使用它相同的类型。我不知道'section.Type'是什么,但是'Int'显然是一个整数。所以要确定你比较的是同一类型的。你可以随时在一个变量上“Ctrl + click”来查看它的类型。 fahmidin? – Honey

+0

@fatemeh部分是indexPath.section。我认为这很明显。我编辑了代码。 – Gabriel

1

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

  • 创建单元格类型的枚举

    enum CellType { 
        case one, two 
    } 
    
  • CellData struct包含节的标题,单元格类型的数组以及文本字符串的数组。

    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 
    } 
+0

你的答案有'dequeueReusableCellWithIdentifier'。她的不是!有没有什么办法,甚至可以没有这样的工作?! – Honey

+1

我特意编写了可重用单元的代码,因为它更容易实现。不幸的是,许多教程都提出了更繁琐(和古老)的方法。 – vadian

相关问题