gpt4 book ai didi

ios - iOS 8 上 UITableView 中的标签重叠,但 iOS 7 上不重叠

转载 作者:行者123 更新时间:2023-11-30 14:04:32 24 4
gpt4 key购买 nike

如果我在 iOS8 上运行我的应用程序而不是在 iOS 7 上运行我的应用程序,标签 (lblscore) 似乎会重叠。当我检查它时,cell.addSubviews(lblscore) 导致了它,但我不能删除它,因为如果我这样做,该标签将根本不会显示。我没有使用 Storyboard。

这是我在 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
}

最佳答案

我希望这对遇到这种情况的人有所帮助。我自己找到了解决方案。由于我的应用程序可以在 iOS 8 和 iOS 7 中运行,因此首先要做的是识别版本,然后识别单元格。要知道单元格是否为空,“cell == nil”永远不会起作用,因为在 iOS8 中单元格已经有 2 个 subview (内容 View 和分隔符)。在 iOS 7 中,在获取单元格之前,您必须先检查它的包装器。

进一步解释...

let version = "\(Array(String(UIDevice.currentDevice().systemVersion))[0])"//get device version
if (version == "7"){//if iOS 7
for wrapper in cell.subviews{//go to the wrapper
if (wrapper.subviews.count==2){//if the wrapper contains the content view and the separator only
//Here you will add the contents of the cell
}
else{//if wrapper has more subview which indicate that it is already filled
var i = 0
for sub in wrapper.subviews{//scan all subviews of wrapper
i++
if (i>2){//delete items 3 and beyond since the content view and separator is in the first 2
sub.removeFromSuperview()//remove them
}
}
//Here you will add the contents of the cell
}
}
}
else{//if iOS 8
if (cell.subviews.count==2){//if the cell contains the content view and the separator only
//Here you will add the contents of the cell
}
else{//if cell has more subview which indicate that it is already filled
var i = 0
for sub in cell.subviews{//scan all subviews of wrapper
i++
if (i>2){//delete items 3 and beyond since the content view and separator is in the first 2
sub.removeFromSuperview()//remove them
}
}
//Here you will add the contents of the cell
}
}
}

现在,当表格尝试重用某个单元格时,它会在添加新内容之前删除该单元格的内容。我希望这会对很多人有所帮助。

关于ios - iOS 8 上 UITableView 中的标签重叠,但 iOS 7 上不重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576874/

24 4 0