gpt4 book ai didi

iOS UITableView 使用自定义 UITableViewCell 时为空

转载 作者:行者123 更新时间:2023-11-30 13:08:02 25 4
gpt4 key购买 nike

我正在尝试在 iOS 上使用 UITableView 制作一个列表,但是当我使用自定义单元格时,它始终为空并显示空单元格,并且不在我设置的计数中。

ViewController.swift

import UIKit

class ViewController: UITableViewController {

var collections = ["Collection 1", "Collection 2", "Collection 3", "Collection 4", "Collection 5", "Collection 6"]
var descriptions = ["Collection description 1", "Collection description 2", "Collection description 3", "Collection description 4", "Collection description 5", "Collection description 6"]
let cellIdentifier = "CollTableViewCell"

override func viewDidLoad() {
super.viewDidLoad()
let tableNib = UINib(nibName:"CollectionTableViewCell", bundle:nil)
tableView.registerClass(CollectionTableViewCell.self, forCellReuseIdentifier: cellIdentifier)
tableView.registerNib(tableNib,forCellReuseIdentifier:cellIdentifier)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionTableViewCell
cell.collectionName?.text = collections[indexPath.row]
return cell
}

}

CollectionTableViewCell.swift

import UIKit

class CollectionTableViewCell: UITableViewCell {

@IBOutlet weak var collectionName: UILabel!
@IBOutlet weak var collectionDesc: UILabel!
@IBOutlet weak var collectionImage: UIImageView!

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}

override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

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
}

}

请帮我解决这个问题。

最佳答案

class ViewController: UIViewController {

@IBOutlet weak var tblView: UITableView!
var collections:NSArray = []
var descriptions:NSArray = []

override func viewDidLoad() {
collections = ["Collection 1", "Collection 2", "Collection 3", "Collection 4", "Collection 5", "Collection 6"]
descriptions = ["Collection description 1", "Collection description 2", "Collection description 3", "Collection description 4", "Collection description 5", "Collection description 6"]
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return collections.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
cell.lbl_Name.text = collections.objectAtIndex(indexPath.row) as? String
cell.lbl_Desc.text = descriptions.objectAtIndex(indexPath.row) as? String
return cell
}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}


class TableViewCell: UITableViewCell {

@IBOutlet weak var lbl_Name: UILabel!
@IBOutlet weak var lbl_Desc: 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
}

}

关于iOS UITableView 使用自定义 UITableViewCell 时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39107386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com