2016-11-01 47 views
0

我使用swift爲iPad創建iOS應用程序。每個視圖都會有表格視圖,我創建了UITableView類,但我無法查看任何數據。我已經將其他視圖中的tableview鏈接到了該自定義類。iOS swift with tableview自定義類

import UIKit 

class SideTable: UITableView, UITableViewDataSource, UITableViewDelegate { 

var TableItems = ["...", "Dashboard"] 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return TableItems.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = dequeueReusableCell(withIdentifier: TableItems[indexPath.row])! as UITableViewCell 

    cell.textLabel?.text = TableItems[indexPath.row] 
    cell.textLabel?.numberOfLines = 0 
    cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping 


    // let currentpagetitle = self.navigationItem.title?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
    let cellname = cell.reuseIdentifier?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 

//  if (cellname == currentpagetitle) 
//  { 
// 
//   cell.backgroundColor = UIColor.lightGray 
//  } 
//   

    return cell 
} 

回答

0

您是否連接了數據源和委託?把你的viewwilllappear ,例如(或viewDidLoad中):

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    self.tableview.datasource = self 
    self.tableview.delegate = self 
} 

或者,如果您使用的是故事板或XIB 控制+點擊過的tableview,並與店主聯繫,然後selecte代表和重複數據源

它連接

如果這不是問題,也許你對單元格的定義有問題。更改您的出列單元:

let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell 

如果單元格相似,請使用相同的標識符。現在您使用的名稱不同,您正在爲每個單元格使用TableItems [indexPath.row]