2016-08-08 8 views
1

誰もがここで何が間違っているのか分かりませんか?UITableViewCellがラベルを表示していません

class ViewController: UIViewController, UITableViewDelegate , UITableViewDataSource { 

let containeView = UIView() 
var tableView = UITableView() 

let mArray = ["HELLO","FROM","THE","OTHER","SIDE"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    containeView.frame = CGRectMake(20, 20, self.view.frame.size.width - 40, self.view.frame.size.height - 40) 
    tableView = UITableView(frame: containeView.bounds, style: .Plain) 
    containeView.backgroundColor = UIColor.purpleColor() 
    containeView.center = self.view.center 
    containeView.addSubview(tableView) 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    tableView.reloadData() 


    view.addSubview(containeView) 

    tableView.reloadData() 
} 


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

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

    return 5 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? UITableViewCell 

     cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell") 
     cell?.backgroundColor = UIColor.greenColor() 
     cell?.textLabel?.text = mArray[indexPath.row] 
     cell?.backgroundColor = UIColor.redColor() 
     cell?.textLabel?.textColor = UIColor.blackColor() 
     cell?.backgroundColor = UIColor.blueColor() 
     print("CELL") 


    return cell! 
} 
} 

私はtextLabel年代の色を設定しようとしました:私は私のParentViewのサブビュー内部のtableViewを追加しようとしています、UITableViewがある...私のコードを(私はそれをスクロールすることができますが、細胞は、ラベルテキストを表示されていません)テキストも、セルの背景色を設定しようとしましたが、何もラベルの状態を追加する必要がUPDATE(解決しよう)

(私は、セルを選択していたときにラベルが表示されている)私のため


を働いていません

誰もが実際にここで何が起こっているのか説明することができます、私は行1を選択することはできません、そして私は私の行を選択しているときにそれはグレーに変わり、前に選択した行が青に変わり、私は任意の行を選択するまで、彼らは再び見えなくなる画面から消えて(オフセット)?

enter image description here

+0

'印刷( "CELL")'印刷コンソールまたはしないようにコードを変更してみ? –

+0

なぜ、alloc/initの直後にセルをデキューするのですか? – Larme

+0

プリントセルがきれいに印刷されています@BhavinRamani –

答えて

2

この

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
    if cell == nil { 
     cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell") 
     cell?.backgroundColor = UIColor.greenColor() 
    } 

    cell?.textLabel?.text = mArray[indexPath.row] 
    cell?.textLabel?.textColor = UIColor.blackColor() 
    print("CELL") 

    return cell! 
} 
関連する問題