无法在模拟器中看到表观和自定义单元格
我是Xcode的新手,并试图完成一个我有2个视图控制器的课程的作业。我没有问题在第一个视图控制器上看到我的表观视图,但无法在模拟器中的第二视图控制器中看到表视图。
我为表视图提供了代表和数据源插座,以及第二视图控制器类中的其他类,以链接自定义单元格的插座。
第一个视图控制器显示了专辑列表。如果添加或编辑,则在第二视图控制器中有一个segue,该控制器允许对专辑进行更改,并在底部有一个表视图,可以添加或编辑轨道。如您在屏幕截图中所看到的那样,我没有任何问题。
这是我的第二视图控制器的代码(COLLEDETAILVIEWCONTROLLER):
class AlbumDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tracks: [TrackObject] = []
var albumViewController: AlbumViewController!
var itemIndex = 0
var cell: TrackCell!
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var artistTextField: UITextField!
@IBOutlet weak var yearTextField: UITextField!
@IBOutlet weak var labelTextField: UITextField!
@IBOutlet weak var reviewTextField: UITextField!
@IBOutlet weak var trackListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
titleTextField.text = albumViewController.albums[itemIndex].title
artistTextField.text = albumViewController.albums[itemIndex].artist
yearTextField.text = albumViewController.albums[itemIndex].year
labelTextField.text = albumViewController.albums[itemIndex].label
reviewTextField.text = albumViewController.albums[itemIndex].review
}
@IBAction func savePressed(_ sender: UIButton) {
albumViewController.albums[itemIndex].title = titleTextField.text!
albumViewController.albums[itemIndex].artist = artistTextField.text!
albumViewController.albums[itemIndex].year = yearTextField.text!
albumViewController.albums[itemIndex].label = labelTextField.text!
albumViewController.albums[itemIndex].review = reviewTextField.text!
albumViewController.albumTableView.reloadData()
}
@IBAction func cancelPressed(_ sender: UIButton) {
if albumViewController.newFlag {
albumViewController.albums.remove(at: itemIndex)
albumViewController.albumTableView.reloadData()
albumViewController.newFlag = false
}
}
@IBAction func pressedEditTrack(_ sender: UIButton) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tracks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "trackListCell", for: indexPath) as! TrackCell
let track = tracks[indexPath.row]
cell.title?.text = track.title
cell.number?.text = track.number
cell.length?.text = track.length
return cell
}
}
class TrackCell: UITableViewCell {
@IBOutlet weak var title: UITextField?
@IBOutlet weak var number: UITextField?
@IBOutlet weak var length: UITextField?
}
我还有两个其他类,可以存储专辑和曲目的对象。我毫不费力地访问他们的数据,只是看到表观及其单元格。
我已经尝试并通过其他文章进行了研究,但可以找到解决我问题的解决方案。我觉得我在ViewWillAppear方法中缺少一个组件。我还拥有另一个版本的项目,在该项目中,我通过View Controller 2中的另一个SEGUE在第三视图控制器中显示了曲目列表,但它与无法看到表视图的相同问题。感谢任何想法!
I'm new to Xcode and trying to finish an assignment for a class in which I have 2 view controllers. I have no issues seeing my table view on the first view controller but unable to see the table view in the second view controller in the simulator.
I have the delegate and datasource outlets for the table view and an additional class in the 2nd view controller class to link the outlets for the custom cells.
The first view controller shows a list of albums. If adding or editing then there is a segue into a 2nd view controller which allows changes to the album and has a table view at the bottom in which tracks can be added or edited. I have no issue seeing the album details as you can see in the screenshot.
Here's my code for the 2nd view controller(AlbumDetailViewController):
class AlbumDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tracks: [TrackObject] = []
var albumViewController: AlbumViewController!
var itemIndex = 0
var cell: TrackCell!
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var artistTextField: UITextField!
@IBOutlet weak var yearTextField: UITextField!
@IBOutlet weak var labelTextField: UITextField!
@IBOutlet weak var reviewTextField: UITextField!
@IBOutlet weak var trackListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
titleTextField.text = albumViewController.albums[itemIndex].title
artistTextField.text = albumViewController.albums[itemIndex].artist
yearTextField.text = albumViewController.albums[itemIndex].year
labelTextField.text = albumViewController.albums[itemIndex].label
reviewTextField.text = albumViewController.albums[itemIndex].review
}
@IBAction func savePressed(_ sender: UIButton) {
albumViewController.albums[itemIndex].title = titleTextField.text!
albumViewController.albums[itemIndex].artist = artistTextField.text!
albumViewController.albums[itemIndex].year = yearTextField.text!
albumViewController.albums[itemIndex].label = labelTextField.text!
albumViewController.albums[itemIndex].review = reviewTextField.text!
albumViewController.albumTableView.reloadData()
}
@IBAction func cancelPressed(_ sender: UIButton) {
if albumViewController.newFlag {
albumViewController.albums.remove(at: itemIndex)
albumViewController.albumTableView.reloadData()
albumViewController.newFlag = false
}
}
@IBAction func pressedEditTrack(_ sender: UIButton) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tracks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "trackListCell", for: indexPath) as! TrackCell
let track = tracks[indexPath.row]
cell.title?.text = track.title
cell.number?.text = track.number
cell.length?.text = track.length
return cell
}
}
class TrackCell: UITableViewCell {
@IBOutlet weak var title: UITextField?
@IBOutlet weak var number: UITextField?
@IBOutlet weak var length: UITextField?
}
I do also have 2 other classes that store the objects for Albums and Tracks. I have no trouble accessing their data, just seeing the table view and its cells.
I've tried and researched through other postings on SO but am able to find a solution for my issue. I have a feeling that I'm missing a component in my viewWillAppear method. I also have another version of this project in which I showed the track list in a 3rd view controller via another segue from view controller 2 but it has the same issue of not being able to see the table view. Appreciate any thoughts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论