【发布时间】:2015-12-11 04:01:05
【问题描述】:
如果我在 iOS8 上而不是在 iOS 7 上运行我的应用程序,似乎标签 (lblscore) 重叠。当我检查它时,cell.addSubviews(lblscore) 导致了它,但我无法删除它,因为如果我这样做,该标签将根本不显示。我没有使用故事板。
这是我在 cellForRowAtIndexPath 中的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
var i = 0
var lblname = UILabel()
var lblmsg = UILabel()
let row = indexPath.row
var forName = ""
var forScore = 0
if let not_me = threads[row]["not_me"] as? NSDictionary{
if let username = not_me["username"] as? String{
forName = username
}
if let score = threads[row]["partner_score"] as? Int{
forScore = score
}
if (not_me == [:]){
if let username = defaults.valueForKey("username") as? String{
forName = username
}
if let score = threads[row]["author_score"] as? Int{
forScore = score
}
}
}
for sub in cell.subviews{
if (sub.subviews.count>6){
for sub1 in sub.subviews{
i++
if ((i>0) && !(i==7)){
switch (i){
case 3:
lblmsg = sub1 as! UILabel
lblmsg.text = ""
lblmsg.text = threads[row]["content"] as? String
lblmsg.sizeToFit()
case 4:
lblname = sub1 as! UILabel
lblname.text = ""
lblname.sizeToFit()
lblname.text = forName
lblname.sizeToFit()
lblname.frame = CGRectMake(40, 10, lblname.bounds.width, lblname.bounds.height+4)
lblmsg.frame = CGRectMake(40, lblname.bounds.height-5, UIScreen.mainScreen().bounds.width-100,50)
case 5:
let lbldate : UILabel = sub1 as! UILabel
lbldate.text = ""
lbldate.sizeToFit()
lbldate.text = threads[row]["timeAgo"] as? String
lbldate.sizeToFit()
lbldate.frame = CGRectMake(UIScreen.mainScreen().bounds.width/3*2, 15, lbldate.bounds.width, 15)
case 6:
let lblscore : UILabel = sub1 as! UILabel
lblscore.text = ""
lblscore.sizeToFit()
lblscore.text = "\(forScore)"
lblscore.sizeToFit()
if(lblscore.bounds.width == 15){
lblscore.frame = CGRectMake(lblname.bounds.width+50, 10, lblscore.bounds.width+5, lblscore.bounds.height+4)}
else{
lblscore.frame = CGRectMake(lblname.bounds.width+50, 10, lblscore.bounds.width+10, lblscore.bounds.height+4)}
default:
print("")
}
}
if (i==7){
let btnDelete = sub1 as! UIButton
btnDelete.tag = row
}
}
}
else{
cell.alpha = 0
let lblscore = UILabel()
let btnDelete = UIButton(frame: CGRectMake(UIScreen.mainScreen().bounds.width-18, 22.5, 22, 30))
btnDelete.tag = row
btnDelete.addTarget(self, action: "openDeleteBar:", forControlEvents: UIControlEvents.TouchUpInside)
lblscore.backgroundColor = UIColor(red: (102/255), green: (193/255), blue: (144/255), alpha: 1)
btnDelete.backgroundColor = UIColor(patternImage: UIImage(named: "grayBin")!)
lblname.text = forName
lblname.sizeToFit()
lblscore.text = "\(forScore)"
lblscore.sizeToFit()
lblname.frame = CGRectMake(40, 10, lblname.bounds.width, lblname.bounds.height+4)
let lblmsg = UILabel(frame: CGRectMake(40, lblname.bounds.height-5, UIScreen.mainScreen().bounds.width-100,50))
lblmsg.text = threads[row]["content"] as? String
if(lblscore.bounds.width == 15){
lblscore.frame = CGRectMake(lblname.bounds.width+40, 10, lblscore.bounds.width+5, lblscore.bounds.height+4)}
else{
lblscore.frame = CGRectMake(lblname.bounds.width+40, 10, lblscore.bounds.width+10, lblscore.bounds.height+4)}
lblscore.layer.borderWidth = 1
lblscore.clipsToBounds = true
lblscore.layer.cornerRadius = 10
lblscore.textAlignment = .Center
let lbldate = UILabel()
lbldate.text = threads[row]["timeAgo"] as? String
lbldate.sizeToFit()
lbldate.frame = CGRectMake(UIScreen.mainScreen().bounds.width/3*2, 15, lbldate.bounds.width, 15)
lblname.font = UIFont(name: "Roboto", size: 15)
lblmsg.font = UIFont(name: "Roboto", size: 13)
lbldate.font = UIFont(name: "Roboto", size: 13)
lblscore.font = UIFont(name: "Roboto", size: 15)
lbldate.textColor = UIColor.lightGrayColor()
lblmsg.textColor = UIColor.lightGrayColor()
lblscore.textColor = UIColor.whiteColor()
cell.selectionStyle = .None
cell.addSubview(lblmsg)
cell.addSubview(lblname)
cell.addSubview(lbldate)
cell.addSubview(lblscore)
cell.addSubview(btnDelete)
UIView.animateWithDuration(0.2, delay: 0, options: nil, animations: {
cell.alpha = 1
}, completion: nil)
}
}
return cell
}
【问题讨论】:
-
从可读性的角度来看,您应该在
let/var语句中使用驼峰命名法。此外,给您的let/vars 提供更具描述性的名称也将大大有助于提高可读性。例如:let labelScore = UILabel()而不是let lblscore = UILabel() -
我的眼睛!他们燃烧!你在用故事板吗?如果是这样,请使用具有自动布局和约束的原型单元。根据屏幕边界设置东西很容易失败。
-
用故事板中的自定义单元子类和原型单元替换所有代码。此外,不要使用硬编码的字体大小 - 使用动态类型。并且尽量不要使用非标准字体——使用苹果提供的系统字体。
-
我没有使用故事板。很抱歉,我没有注意到我这样做时没有使用驼峰箱,因为它很忙,但是我太累了,无法更换它。字体来自客户端,所以我没有保留它。
-
嗯,
Roboto字体...您是从 android 移植代码吗? ;)
标签: ios swift uitableview ios7 ios8