我是 Swift 的新手。我正在尝试在UIScrollView中创建UILabel
。由于某些原因,不显示文本。我确定标签已添加到scrollview中,因为我设置了框架的背景颜色,我可以看到它们显示在scrollview中。单步执行代码时,我可以看到标签的text属性被更新为我传递给我的方法的字符串,但是我看不到文本。我很茫然。有没有人有任何想法?谢谢!
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
@IBOutlet weak var winLoseValueLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var newTotalLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var playersScrollView: CustomTableCellScrollView!
@IBOutlet weak var scrollContentView: UIView!
var numLinesInScrollView : CGFloat?
var tblView : UITableView?
var people : String?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersScrollView.tblView = tblView
numLinesInScrollView = 0.0
self.playersScrollView.contentSize.width = self.scrollContentView.frame.width
self.playersScrollView.contentSize.height = 100
self.playersScrollView.addSubview(self.scrollContentView)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
// Increment the number of lines in the scrollview
if numLinesInScrollView != nil {
numLinesInScrollView!++
}
else{
numLinesInScrollView = 1
}
// Get the bounds of the screen
let screenSize : CGRect = UIScreen.mainScreen().bounds
// Update the height of the scrollview
self.playersScrollView.contentSize.height = 20 * numLinesInScrollView!
// Add a new label to the players scroll view
let w : CGFloat = self.scrollContentView.frame.width - 350
let h : CGFloat = 20
let x : CGFloat = 0
let y : CGFloat = (numLinesInScrollView! - 1) * h
let frame : CGRect = CGRect(x: x, y: y, width: w, height: h)
var person : UILabel = UILabel(frame: frame)
person.font = UIFont(name: "HelveticaNeue-Bold", size: 12.0)
person.textColor = UIColor.blackColor()
person.textAlignment = NSTextAlignment.Center
switch numLinesInScrollView!{
case 1:
person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
case 2:
person.backgroundColor = UIColor(red: 0, green: 1.0, blue: 0, alpha: 1.0)
case 3:
person.backgroundColor = UIColor(red: 0, green: 0, blue: 1.0, alpha: 1.0)
case 4:
person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 1.0, alpha: 1.0)
default:
break
}
person.text? = str
self.scrollContentView.addSubview(person)
}
}
修改:我在str
中设置UITableViewController
。这是代码。
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataMgr.data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = self.tblView.dequeueReusableCellWithIdentifier("ledgerCell", forIndexPath : indexPath) as! CustomTableViewCell
var values = dataMgr.data[indexPath.row]
cell.newTotalLabel?.text = "\(values.newTotal)"
cell.winLoseValueLabel?.text = "\(values.newTotal - values.currentTotal)"
cell.dateLabel?.text = "5/17/2015"
cell.addLabelToScrollView("John Doe")
cell.addLabelToScrollView("Jane Doe")
cell.addLabelToScrollView("Bob Test")
cell.addLabelToScrollView("Joe Test")
cell.tblView = self.tblView
return cell
}
}
答案 0 :(得分:0)
我从?
中移除了person.text? = str
并解决了问题。