Я новичок в разработке iOS, и я пытаюсь создать пользовательские tableViewCells, но у меня есть проблема. У меня есть 3 раздела для моего TableViewCell и Array. Когда я пытаюсь назначить значения ячейкам по адресу UITableView, cellForRowAt
, он запускает массив сверху с каждого раздела. Вот мой код:Custom 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 ""
}
}
Не используйте старые способы кодирования, такие как «Bundle.main.loadNibNamed», вместо этого используйте «registerNib», которые помогут вам в создании ячеек. Кроме того, можете ли вы опубликовать снимок экрана о своей проблеме? – Satyam
В 'cellForRowAtIndexPath' вы рассматриваете только строку указательного пути. Поэтому каждый раздел отображает те же объекты из верхней части массива. Кроме того, вы можете создавать несколько настраиваемых ячеек в Interface Builder (я думаю, начиная с Xcode 6), нет необходимости в дополнительных наконечниках. И, наконец, не перекодируйте 'numberOfRows'. Создайте подходящую модель с вложенными массивами или чем-то еще, чтобы всегда использовать свойство count. – vadian
Вы не можете просто рассмотреть 'indexPath.row', вычислить индекс массива на основе' indexPath.section' и 'indexPath.row'. Затем вы получите точный элемент из массива. –