2016-06-12 91 views
-1

我目前正在研究一个需要将标签内容加载到表视图控制器的应用程序。我创建了一个我想填充表视图的字符串值数组,但由于某些原因,内容没有显示在模拟器中。我没有收到任何错误,但数据似乎没有注册。有关我如何解决这个问题的任何想法?试图填充tableview但是标签内容不会在模拟器中显示

下面是我的表视图控制器文件:

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

为什么你期望你的数据出现在Interface Builder中?它只会出现在你正在运行的应用程序中。 – rmaddy

+0

它也不会显示在应用模拟器中:/ –

+0

调试器在使用时会显示什么内容?正确的方法被调用? 'tableView:numberOfRowsInSection:'和'tableView:cellForRowAtIndexPath:'返回正确的值吗?你有没有检查你的'cellIdentifier'在IB和你的代码中都是正确的?基本上你在调试中排除了什么问题? –

回答

1
self.tableView.registerClass(catgeoriesTableViewCell.self, forCellReuseIdentifier: "categoriesTableViewCell") 

此行对您造成问题。

如果您从nib或xib加载单元,则首先需要加载nib然后注册它。 nibname必须与文件名和恢复ID相匹配。

Restoration

let cellNib = UINib(nibName: "LanguagePickerCellView", bundle: nil) 
tableView.registerNib(cellNib, forCellReuseIdentifier: "reuseIdentifier") 

如果您使用的原型细胞直接在界面生成器,那么你就必须把reuseIdentifier在故事板。

ReuseIdentifier

如果您使用的是的UITableViewController直接那么你并不需要连接数据源和委托,它是自动完成的。如果你使用普通的TableView,那么你必须链接dataSource和委托。