gpt4 book ai didi

ios - 自定义 UITableView 单元格 Nib 文件仅在选择单元格后显示?

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:48 25 4
gpt4 key购买 nike

当我加载我的应用程序时 this is what happens.由于某种原因,表格 View 加载为空白。只有当我选择单元格并单击另一个单元格时,才会显示之前选择的单元格。但是对于第一个单元格,无论我在哪里/如何点击,我仍然看不到第一个单元格。

我不明白为什么会这样 b/c 我关注了 this tutorial.显然我做错了什么,如果有人能告诉我为什么会这样以及如何解决它,我将不胜感激。

在主 Storyboard 中,我使用了 tableViewController SummaryTableViewController 和自定义 UITableViewCell DayOfWeekTableViewCell。我有一个用于 TableView 单元格的 nib 文件,名为 DayofWeekSpendingTableViewCell.xibHere's a list of all my files and their fileneames.

这是我的 SummaryTableViewController 代码:

class SummaryTableViewController: UITableViewController {
var dayOfWeek: [String] = [String]()
var totalSpentPerDay: [Double] = [Double]()

override func viewDidLoad() {
super.viewDidLoad()

dayOfWeek = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
totalSpentPerDay = [0, 7.27, 0, 0, 39, 0, 0]
}

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

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! DayOfWeekTableViewCell

// Configure the cell...
let nib = NSBundle.mainBundle().loadNibNamed("DayofWeekSpendingTableViewCell", owner: self, options: nil)
cell = nib[0] as! DayOfWeekTableViewCell

cell.dayOfWeek.text = dayOfWeek[indexPath.row]
cell.totalAmountSpent.text = String(totalSpentPerDay[indexPath.row])

return cell
}
}

这是我的自定义单元格 DayOfWeekTableViewCell 代码:

class DayOfWeekTableViewCell: UITableViewCell {

@IBOutlet weak var dayOfWeek: UILabel!
@IBOutlet weak var totalAmountSpent: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

}

在主 Storyboard 中,我...

在 nib 文件 DayofWeekSpendingTableViewCell.xib 中,我...

最佳答案

不是在 cellForRowAtIndexPath 中加载 NIB,而是在 viewDidLoad() 中加载 NIB 并将其注册以用于您的 TableView

override func viewDidLoad() {
super.viewDidLoad()

dayOfWeek = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
totalSpentPerDay = [0, 7.27, 0, 0, 39, 0, 0]
// Create a nib for reusing
let nib = UINib(nibName: "DayofWeekSpendingTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "summaryCell")
}

然后您可以直接访问 cellForRowAtIndexPath 中的单元格:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! DayOfWeekTableViewCell
cell.dayOfWeek.text = dayOfWeek[indexPath.row]
cell.totalAmountSpent.text = String(totalSpentPerDay[indexPath.row])

return cell
}

关于ios - 自定义 UITableView 单元格 Nib 文件仅在选择单元格后显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662699/

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