热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在iOS8上的UITableView中重叠标签,但在iOS7中没有重叠标签-OverlappingLabelinUITableViewoniOS8butnotiniOS7

Itseemsthatalabel(lblscore)isoverlappingifIrunmyapponiOS8butnotiniOS7.WhenIche

It seems that a label (lblscore) is overlapping if I run my app on iOS8 but not in iOS 7. When I check it, the cell.addSubviews(lblscore) caused it but I cannot delete it because if I do, that label will not be displayed at all. I'm not using a storyboard.

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

this is my codes in cellForRowAtIndexPath

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

1 个解决方案

#1


0  

I hope this will help anyone with this situation. I found the solution by myself. Since my app can run in iOS 8 and iOS 7, the first thing to do is identify the version then identify the cell. To know if the cell is empty ,'cell == nil' will never work since a cell already have 2 subviews (content view and separator) in iOS8. In iOS 7 before you get the cell you have to go through it's wrapper.

我希望这可以帮助处理这种情况的任何人。我自己找到了解决方案。由于我的应用程序可以在iOS 8和iOS 7中运行,因此要做的第一件事是确定版本然后识别单元格。要知道单元格是否为空,'cell == nil'将永远不会工作,因为一个单元格在iOS8中已经有2个子视图(内容视图和分隔符)。在获得单元格之前的iOS 7中,您必须通过它的包装器。

to further explain...

进一步解释......

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
        }
    }
}

Now, when the the table tries to reuse a cell, it will delete the contents of that cell before adding the new contents. I hope this will help many.

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


推荐阅读
  • 在尝试加载支持推送通知的iOS应用程序的Ad Hoc构建时,遇到了‘no valid aps-environment entitlement found for application’的错误提示。本文将探讨此错误的原因及多种可能的解决方案。 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 本文详细介绍了 `org.apache.tinkerpop.gremlin.structure.VertexProperty` 类中的 `key()` 方法,并提供了多个实际应用的代码示例。通过这些示例,读者可以更好地理解该方法在图数据库操作中的具体用途。 ... [详细]
  • 问题场景用Java进行web开发过程当中,当遇到很多很多个字段的实体时,最苦恼的莫过于编辑字段的查看和修改界面,发现2个页面存在很多重复信息,能不能写一遍?有没有轮子用都不如自己造。解决方式笔者根据自 ... [详细]
  • spring boot使用jetty无法启动 ... [详细]
  • Beetl是一款先进的Java模板引擎,以其丰富的功能、直观的语法、卓越的性能和易于维护的特点著称。它不仅适用于高响应需求的大型网站,也适合功能复杂的CMS管理系统,提供了一种全新的模板开发体验。 ... [详细]
  • JUnit下的测试和suite
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文通过C++语言实现了一个递归算法,用于解析并计算数学表达式的值。该算法能够处理加法、减法、乘法和除法操作。 ... [详细]
  • 本文介绍了如何通过C#语言调用动态链接库(DLL)中的函数来实现IC卡的基本操作,包括初始化设备、设置密码模式、获取设备状态等,并详细展示了将TextBox中的数据写入IC卡的具体实现方法。 ... [详细]
  • 本文介绍如何手动实现一个字符串连接函数,该函数不依赖于C语言的标准字符串处理函数,如strcpy或strcat。函数原型为void concatenate(char *dest, char *src),其主要作用是将源字符串src追加到目标字符串dest的末尾。 ... [详细]
  • 本文详细介绍了如何利用 Bootstrap Table 实现数据展示与操作,包括数据加载、表格配置及前后端交互等关键步骤。 ... [详细]
  • 高级缩放示例.就像谷歌地图一样.它仅缩放图块,但不缩放整个图像.因此,缩放的瓷砖占据了恒定的记忆,并且不会为大型缩放图像调整大小的图像.对于简化的缩放示例lookhere.在Win ... [详细]
  • Irish budget airline Ryanair announced plans to significantly increase its route network from Frankfurt Airport, marking a direct challenge to Lufthansa, Germany's leading carrier. ... [详细]
  • 3.[15]Writeaprogramtolistallofthekeysandvaluesin%ENV.PrinttheresultsintwocolumnsinASCIIbet ... [详细]
  • linux网络子系统分析(二)—— 协议栈分层框架的建立
    目录一、综述二、INET的初始化2.1INET接口注册2.2抽象实体的建立2.3代码细节分析2.3.1socket参数三、其他协议3.1PF_PACKET3.2P ... [详细]
author-avatar
mobiledu2502859427
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有