我正在创建一个UITableViewCell
对象,并将其返回到cellForRowAtIndexPath
中的tableView
函数内。我在单元格中有一个UIView
,我想制作圆形。使用this link中的代码,我编写了以下代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let containerDP = UIView()
containerDP.backgroundColor = UIColor.redColor()
// some code
let firstLetter = UILabel()
firstLetter.backgroundColor = UIColor.yellowColor()
firstLetter.text = (data[indexPath.row].UserObject.FirstName! as String).uppercaseString[0]
firstLetter.font = UIFont(name: firstLetter.font.fontName, size: 50)
firstLetter.adjustsFontSizeToFitWidth = true
firstLetter.textAlignment = NSTextAlignment.Center
firstLetter.layer.cornerRadius = firstLetter.frame.size.width / 2;
firstLetter.clipsToBounds = true
containerDP.addSubview(firstLetter)
firstLetter.snp_makeConstraints { (make) -> Void in
make.center.equalTo(containerDP)
make.edges.equalTo(containerDP).inset(UIEdgeInsetsMake(10, 10, 10, 10))
}
// some code
return cell
}
但仍然没有圆形面具:
答案 0 :(得分:0)
试试这个:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let containerDP = UIView()
containerDP.backgroundColor = UIColor.redColor()
// some code
let firstLetter = UILabel()
firstLetter.backgroundColor = UIColor.yellowColor()
firstLetter.text = (data[indexPath.row].UserObject.FirstName! as String).uppercaseString[0]
firstLetter.font = UIFont(name: firstLetter.font.fontName, size: 50)
firstLetter.adjustsFontSizeToFitWidth = true
firstLetter.textAlignment = NSTextAlignment.Center
firstLetter.layer.cornerRadius = firstLetter.frame.size.width / 2;
firstLetter.layer.masksToBounds = true
firstLetter.clipsToBounds = true
containerDP.addSubview(firstLetter)
firstLetter.snp_makeConstraints { (make) -> Void in
make.center.equalTo(containerDP)
make.edges.equalTo(containerDP).inset(UIEdgeInsetsMake(10, 10, 10, 10))
}
// some code
return cell
}
答案 1 :(得分:0)
dequeueReusableCellWithIdentifier
进行tablewView单元格创建和缓存awakeFromNib
方法clipsToBounds = true
应该有效答案 2 :(得分:0)
customtableviewCell
课程。IBOutlet
连接到您的cutomtablviewCell #import
customtablviewCell
tableviewController
然后在cellForRowAtIndexPath
方法内(我在ObjectiveC中,而不是在swift中)调用cutomTableviewcell
,如下所示
customTableviewCell * cell = [您的代码在这里];
之后你的风格就像
cell.fisrtLetter.layer.cornerRadius = cell.firstletter.frame.size.width / 2; cell.firstLetter.layer.masksToBounds = true;
请明白。