有人知道这里有什么问题吗?我试图在我的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行,当我选择我的行时,它变成灰色,当我选择任何其他行时,先前选择的行变成蓝色,并且当我滚动并消失时从屏幕(偏移)开始,它们再次变得不可见,直到我选择任何行,任何线索?
最佳答案
尝试将您的代码修改为此
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/