热门标签 | 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.

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


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
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社区 版权所有