ios - UITableViewCell没有显示标签

标签 ios uitableview swift2

有人知道这里有什么问题吗?我试图在我的ParentView的subView内添加一个tableView,那里是UITableView(我可以滚动它,但是Cells没有显示标签文本)...我的代码:

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的颜色,也尝试设置单元格的背景色,但对我没有用

更新(已解决)
需要为Label添加状态(当我选择单元格时会出现标签)

谁能解释我这里实际发生的事情,我无法选择第1行,当我选择我的行时,它变成灰色,当我选择任何其他行时,先前选择的行变成蓝色,并且当我滚动并消失时从屏幕(偏移)开始,它们再次变得不可见,直到我选择任何行,任何线索?

enter image description here

最佳答案

尝试将您的代码修改为此

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

关于ios - UITableViewCell没有显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825374/

相关文章:

ios - 将图像与顶部对齐,在 Swift 2 iOS 9 中显示图像的一部分

swift - 遵守协议(protocol)的误解

ios - 如何在 iPhone 中创建像 youtube 应用程序这样的 float 播放器?

ios - 在 objective-c 中如何获取第 n 个字符?

ios - iOS 中的可操作通知仅是本地通知还是也可以从服务器发送?

ios - 如何在 UITableView 单元格中居中 UIImage?

ios - 没有动画的deleteRowsAtIndexPaths

ios - 使用 Swift 连接方法修改倒数第二个连接器?

ios - Swift - 选择时的 UITableView 单元格动画

Swift:为什么 CGImageCreateWithImageInRect() 会创建一个大 1 像素的矩形?