我目前正在开发一个需要将标签内容加载到表视图控制器的应用程序。我创建了一个字符串值数组,我想填充表视图但由于某种原因,内容没有出现在模拟器中。我没有收到任何错误,但数据似乎没有注册。有关如何解决此问题的任何想法?
下面是我的表视图控制器文件:
categoryTableViewController.swift
// Style Guide
//
// Created by Claudia Laurie on 5/30/16.
// Copyright © 2016 Claudia Laurie. All rights reserved.
//
import UIKit
//struct Color {
// let red, green, blue: Double
// init(red: Double, green: Double, blue: Double) {
// self.red = red
// self.green = green
// self.blue = blue
// }
// init(white: Double) {
// red = white
// green = white
// blue = white
// }
//}
I
class clothing_category: NSObject {
let name: String!
init (name: String) {
self.name = name
}
}
class categoryTableViewController: UITableViewController {
// MARK: Properties
// Create an array to hold the clothing.
var clothing_categories = [clothing_category]()
override func viewDidLoad() {
super.viewDidLoad()
// Register cells? let me google...is this why nothing was showing up in the cells themselves despite loading the data? maybe
self.tableView.registerClass(catgeoriesTableViewCell.self, forCellReuseIdentifier: "categoriesTableViewCell")
// Load sample data
loadSampleClothing_categories()
}
func loadSampleClothing_categories() {
clothing_categories.append(clothing_category(name:"Jackets"))
clothing_categories.append(clothing_category(name: "Accessories"))
clothing_categories.append(clothing_category(name: "Pants"))
clothing_categories.append(clothing_category(name: "Color"))
clothing_categories.append(clothing_category(name: "Tops"))
clothing_categories.append(clothing_category(name: "Dressing for an Occaision"))
// Load up the array when the view is loaded.
clothing_categories.append(clothing_category(name:"Jackets"))
clothing_categories.append(clothing_category(name: "Accessories"))
clothing_categories.append(clothing_category(name: "Pants"))
clothing_categories.append(clothing_category(name: "Color"))
clothing_categories.append(clothing_category(name: "Tops"))
clothing_categories.append(clothing_category(name: "Dressing for an Occaision"))
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return clothing_categories.count
}
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "categoriesTableViewCell"
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// This is the line that is failing, lets find out why
let cell = tableView.dequeueReusableCellWithIdentifier("categoriesTableViewCell", forIndexPath: indexPath) as! catgeoriesTableViewCell
//Fetches the appropriate clothing_catgeory for the data source layout.
let category = clothing_categories[indexPath.row]
cell.nameclothing_category.text = category.name
return cell
}
我相信我也已正确创建了与数据源和委托的出口连接。
答案 0 :(得分:1)
self.tableView.registerClass(catgeoriesTableViewCell.self, forCellReuseIdentifier: "categoriesTableViewCell")
这一行给你带来了麻烦。
如果从nib或xib加载单元格,则首先需要加载nib然后进行注册。 nibname必须匹配文件名和恢复ID。
let cellNib = UINib(nibName: "LanguagePickerCellView", bundle: nil)
tableView.registerNib(cellNib, forCellReuseIdentifier: "reuseIdentifier")
如果您直接在界面构建器中使用原型单元格,那么您只需将reuseIdentifier放在故事板中。
如果您直接使用 UITableViewController ,则无需链接dataSource和委托,它会自动完成。如果您使用普通的TableView,那么您必须链接dataSource和委托。