I'm new to iOS development and I'm trying to create custom tableViewCells but I've got a problem. 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.
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 ""
}
}
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
}