Я новичок в Swift и разработке iOS в целом. Я пытаюсь создать пользовательский UITableViewCell. Я создал ячейку в моей главной раскадровки поверх UITableView, которая находится внутри UIViewController. Когда я загрузил одну из ячеек по умолчанию, я смог заполнить ее данными. Однако теперь, когда я использую пользовательскую ячейку, я не могу получить какие-либо данные в таблице. Я просмотрел всевозможные учебники и вопросы, размещенные в Интернете, но я не могу понять, почему он не работает. Любая помощь будет оценена по достоинству.Swift Custom UITableViewCell не отображает данные из массива
Вот мой код UIViewController, что TableView находится в
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var name = ["Thinh", "Nhut", "Truong", "Tuyet", "Hoai", "Hoang", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"]
var ngaysinh = ["10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991"]
var namsinh = ["1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return name.count
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.hoten.text = name [indexPath.row]
cell.ngaysinh.text = ngaysinh [indexPath.row]
cell.namsinh.text = namsinh [indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
print(name[row])
print(ngaysinh[row])
print(namsinh[row])
}
}
И это CustomCell Класс:.
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var hoten: UILabel!
@IBOutlet weak var ngaysinh: UILabel!
@IBOutlet weak var namsinh: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Можете ли вы поделиться кодом для CustomCell? –
В раскадровке проверьте, что делегат 'tableView' и dataSource подключены к ViewController. –
Я проверяю ссылку oulet, подключается к viewcontroller, но источник данных и делегат не имеют ничего. Это верно? – ThinhLe