2017-02-27 93 views
-1

我寫了一個應用程序,它在UIViewController中一個UITableView,這裏是我的代碼:
的UITableView不顯示任何

class CategorySelectViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var melliSubCategories = [String]() 
var mazhabiSubCategories = [String]() 
var sayerSubCategories = [String]() 


@IBOutlet weak var melliButton: UIButton! 
@IBOutlet weak var sayerButton: UIButton! 
@IBOutlet weak var mazhabiButton: UIButton! 


@IBAction func melliButtonClicked(_ sender: UIButton) { 
    categorySelected = 6 
    melliButton.isHighlighted = true 
    mazhabiButton.isHighlighted = false 
    sayerButton.isHighlighted = false 
    categoryTableView.reloadData() 
} 

@IBAction func sayerButtonClicked(_ sender: UIButton) { 
    categorySelected = 5 
    melliButton.isHighlighted = false 
    mazhabiButton.isHighlighted = false 
    sayerButton.isHighlighted = true 
    categoryTableView.reloadData() 
} 

@IBAction func mazhabiButtonClicked(_ sender: UIButton) { 
    categorySelected = 4 
    melliButton.isHighlighted = false 
    mazhabiButton.isHighlighted = true 
    sayerButton.isHighlighted = false 
    categoryTableView.reloadData() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 


    // Do any additional setup after loading the view. 
    categoryTableView.dataSource = self 
    categoryTableView.delegate = self 
    categoryTableView.register(CategorySelectTableViewCell.self, forCellReuseIdentifier: "Cell") 
    melliSubCategories = DataBaseManager.shared.subCategories(6) 
    mazhabiSubCategories = DataBaseManager.shared.subCategories(4) 
    sayerSubCategories = DataBaseManager.shared.subCategories(5) 
    print(melliSubCategories) 
    print("/////////////////") 
    print(mazhabiSubCategories) 
    print("/////////////////") 
    print(sayerSubCategories) 
    print("/////////////////") 
} 

@IBOutlet weak var categoryTableView: UITableView! 


func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch categorySelected { 
    case 4: //mazhabi 
     return mazhabiSubCategories.count 
    case 5: //sayer 
     return sayerSubCategories.count 
    case 6: //melli 
     return melliSubCategories.count 
    default: 
     return melliSubCategories.count 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CategorySelectTableViewCell 
    cell.label?.text = melliSubCategories[indexPath.row] 

    return cell 

} 

我創建一個用於細胞的表視圖,他們有一個名爲CategorySelectTableViewCell類圖像和標籤。

在代碼中,我通過數據庫填充數組,我想在表格視圖中顯示它們,但tableview不顯示任何內容。

截圖:my storyboarddemo

+0

你是否在自定義單元格中使用xib? – iParesh

+0

如果您打印您要返回的計數值,您看到了什麼?如果您打印標籤上的內容,您看到了什麼? –

+0

@iParesh否我不使用xib,我通過表格視圖單元格創建自定義單元格 – arash

回答

-1

請檢查陣列計數爲零或不在下面numberOfRowsInSection方法。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

另外,交叉檢查小區標識符是否正確。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell : CategorySelectTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CategorySelectTableViewCell 
    cell.label?.text = melliSubCategories[indexPath.row]  
    return cell 
} 

刷新的tableview在viewDidLoad方法:

categoryTableView.reloadData()

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
    categoryTableView.dataSource = self 
    categoryTableView.delegate = self 
    categoryTableView.register(CategorySelectTableViewCell.self, forCellReuseIdentifier: "Cell") 
    melliSubCategories = DataBaseManager.shared.subCategories(6) 
    mazhabiSubCategories = DataBaseManager.shared.subCategories(4) 
    sayerSubCategories = DataBaseManager.shared.subCategories(5) 
    print(melliSubCategories) 
    print("/////////////////") 
    print(mazhabiSubCategories) 
    print("/////////////////") 
    print(sayerSubCategories) 
    print("/////////////////") 
    categoryTableView.reloadData() 
} 
+0

謝謝你的回答,但正如我在評論中所說的數組和打印工作正常,我reloadData按鈕的行動,那裏和viewDidLoad之間有任何區別? – arash

+0

這只是重複OP的問題 –

+0

@AshleyMills什麼是OP? – arash

0

你是在說:

cell.label?.text = melliSubCategories[indexPath.row] 

它,這是不可能的工作。爲了使其工作,您的自定義單元格類型CategorySelectTableViewCell需要位於帶有label插座的筆尖。但是那個筆尖要麼在故事板上,要麼在xib文件中。但你也說

categoryTableView.register(CategorySelectTableViewCell.self, ... 

那行防止從廈門國際銀行或故事板來的細胞。所以電源插座不能工作,電池將保持空置狀態。

+0

我也在想''cell.label?.text'它怎麼可能。最初我以爲他使用本地單元,但後來我滾動代碼,而不是本機。沒有得到他在做什麼。 :/ –

+0

如果我刪除這行應用程序崩潰並說'原因:'無法使用標識符出列單元格 - 必須爲標識符註冊一個筆尖或類或連接故事板'中的原型單元格,但我也設置原型單元的標識 – arash

+0

但是,您的故事板配置不正確。您的視圖控制器沒有正確地在故事板中看到表格視圖。 – matt