简体   繁体   中英

UITableViewCell is not showing Label

Anybody knows whats wrong here ? I am trying to add a tableView Inside my ParentView's subView , UITableView is there (I can scroll it but Cells are not showing the label text)... my code :

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!
}
}

I tried to set the color of textLabel's text also tried to set the background color of cell but nothing worked for me


UPDATE (SOLVED) Needed to add the state for Label (Label is appearing when i'm selecting the cell)

Can anyone explain me what is actually happening here , i can't select row 1 , and when i'm selecting my row it turns to Gray and when i select any other row previous selected row turns to blue and when i scroll and cell disappear from the screen (offset) they again become invisible until i select any row , any clue ??

在此处输入图片说明

Try modify your code to this

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!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM