我有一个带有自定义单元格的TableView控制器,只要加载表格视图,我就想用新数据重新加载。
我遇到的问题是数据正在正确地重新加载但是细胞标签不是。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> JobTableViewCell{
jobSelect = Int(indexPath.row)
var cell: JobTableViewCell = tableView.dequeueReusableCellWithIdentifier("JobTableViewCell") as JobTableViewCell
if indexPath.row % 2 == 0{cell.backgroundColor = UIColor.lightGrayColor()
}
else {cell.backgroundColor = UIColor.whiteColor()}
var job = arrayOfJobs[indexPath.row]
var totalHours = job.monHours + job.tueHours + job.wedHours + job.thuHours + job.friHours + job.satHours + job.sunHours
cell.setCell(job.jobName, income: job.income, commute: job.commute, jobHours: totalHours)
return cell
}
这是自定义单元格 -
import UIKit
class JobTableViewCell: UITableViewCell {
@IBOutlet var jobTitle: UILabel!
@IBOutlet var income: UILabel!
@IBOutlet var commute: UILabel!
@IBOutlet var jobHours: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setCell(jobTitle: String, income: Int, commute: Int, jobHours: Int){
self.jobTitle.text = String(arrayOfJobs[jobSelect
].jobName)
self.income.text = String("Income: $\(arrayOfJobs[jobSelect].income) a month")
self.commute.text = String("Commute: \(arrayOfJobs[jobSelect].commute) hours away from home")
self.jobHours.text = String("Available hours:\(arrayOfJobs[jobSelect].monHours + arrayOfJobs[jobSelect].tueHours + arrayOfJobs[jobSelect].wedHours + arrayOfJobs[jobSelect].thuHours + arrayOfJobs[jobSelect].friHours + arrayOfJobs[jobSelect].satHours + arrayOfJobs[jobSelect].sunHours) hours a week")
}
}
我还有一个第二个视图控制器设置,当选择一个单元格时出现 -
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
jobSelect = Int(indexPath.row)
var JobSelector: jobSubView = storyboard?.instantiateViewControllerWithIdentifier("jobSubView") as jobSubView
self.presentViewController(JobSelector, animated: true, completion: nil)
self.rowSelection = Int(indexPath.row)
}
这是子视图 -
import UIKit
class jobSubView: UIViewController {
@IBOutlet var jobTitle: UILabel!
@IBOutlet var income: UILabel!
@IBOutlet var commute: UILabel!
@IBOutlet var jobHours: UILabel!
@IBOutlet var qualifications: UILabel!
@IBOutlet var jobDescription: UITextView!
@IBOutlet var goBackButton: UIButton!
@IBOutlet var appluButton: UIButton!
@IBAction func applyButtonAction(sender: UIButton) {
override func viewDidLoad() {
super.viewDidLoad()
self.jobTitle.text = String(arrayOfJobs[jobSelect
].jobName)
self.income.text = String("Income: $\(arrayOfJobs[jobSelect].income) a month")
self.commute.text = String("Commute: \(arrayOfJobs[jobSelect].commute) hours away from home")
self.jobHours.text = String("Available hours: \(arrayOfJobs[jobSelect].monHours + arrayOfJobs[jobSelect].tueHours + arrayOfJobs[jobSelect].wedHours + arrayOfJobs[jobSelect].thuHours + arrayOfJobs[jobSelect].friHours + arrayOfJobs[jobSelect].satHours + arrayOfJobs[jobSelect].sunHours) hours a week")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
单元格和子视图上的标签来自相同的数据。第一次加载tableview时,单元格和子视图都会正确显示。
当选择单元格时,子视图会加载正确的数据,但是当我返回到tableview时,单元格标签应该更新,但它们不会更新。但子视图标签是。
有什么想法吗?
答案 0 :(得分:0)
所以,我想出来了。您必须在viewWillAppear而不是viewDidLoad中执行self.tableView.reloadData()。