我是iOS开发新手,我正在尝试创建自定义tableViewCells,但我遇到了一个问题。我的tableViewCell有3个部分,Array.When试图为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 ""
}
}发布于 2016-11-30 09:33:18
我用这种方式改变了我的代码,它起作用了!
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
}发布于 2016-11-29 15:01:33
这是一个具有更方便的数据源的建议。
CellData结构包含各节的标题、单元格类型的数组和文本字符串的数组。
结构CellData { let标题: String let typeArray : CellType let textArray : String }viewDidLoad中创建三个部分。我为在textArray项目中使用西方文本而道歉,因为我对从右到左的文本行为太困惑了。
重写func viewDidLoad() { super.viewDidLoad() let section0 =CellData(标题:“تنظیماترنگ\n”,typeArray:.one,.two,.one,textArray:"Text 1","Text 2","Text 3")让section1 =CellData(标题:“تنظیماتسیستم\n”,typeArray:.two,.two,textArray:"Text 4","Text 5","Text 6",)让section2 =CellData(标题:"پشتیبانی\n",typeArray:.two,textArray:"Text 8") arrayOfCellData = section0,section1,section2 }现在,数据源和委托方法更易于填充。
下面的代码假设这两个单元格都是直接在Interface中设计的,具有名为TableViewCell_Type1和TableViewCell_Type2的自定义类以及适当的标识符Type1和Type2
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
}发布于 2016-11-29 14:44:05
不要使用arrayOfCellDataindexPath.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]https://stackoverflow.com/questions/40867951
复制相似问题