在我的UITableViewCell的内容视图中,我有一个标签,我想以编程方式设置框架,但无论我给它什么框架,它都保持不变。我的cellForRowAtIndexPath
- 函数如下所示:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let selectedBackground = UIView(frame: CGRectMake(0, 0, width, 44))
let selectedBackgroundImage = UIImageView(frame: CGRectMake(width - 20, 44/2 - 5, 10, 10))
selectedBackgroundImage.image = UIImage(named: "checked")
selectedBackgroundImage.contentMode = .ScaleAspectFit
selectedBackground.addSubview(selectedBackgroundImage)
let separatorViewRegular:UIView = UIView(frame: CGRectMake(0, 43, 1024, 1))
separatorViewRegular.layer.borderColor = grayColor.CGColor
separatorViewRegular.layer.borderWidth = 1
let cell:CompanyTableViewCell = self.companiesTV.dequeueReusableCellWithIdentifier("cell") as! CompanyTableViewCell
cell.label.textColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
cell.label.frame = CGRectMake(0.7 * width, 0, 0.3 * width, 44)
cell.label.font = UIFont(name: "Avenir", size: 16)
cell.label.adjustsFontSizeToFitWidth = true
cell.label.numberOfLines = 1
cell.label.textAlignment = NSTextAlignment.Left
cell.backgroundColor = UIColor.clearColor()
cell.label.text = tickersToDisplay[indexPath.row]
cell.selectedBackgroundView = selectedBackground
cell.contentView.addSubview(separatorViewRegular)
return cell
}
对于为什么不起作用的任何帮助和建议都将不胜感激。
答案 0 :(得分:0)
我认为您的问题是您没有检查该单元格是否为零。
let cellId = "cell"
var cell : CompanyTableViewCell = self.companiesTV.dequeueReusableCellWithIdentifier(cellId) as? CompanyTableViewCell
if cell == nil {
cell = CompanyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId) // Your constructor here
}