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

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


推荐阅读
  • 实现‘点击恢复’功能 - Tap-to-Resume Feature in SpriteKit
    了解如何在应用程序从非活动状态返回时,在SpriteKit游戏中添加一个‘点击恢复’的文字提示。 ... [详细]
  • 本文详细介绍了Grand Central Dispatch (GCD) 的核心概念和使用方法,探讨了任务队列、同步与异步执行以及常见的死锁问题。通过具体示例和代码片段,帮助开发者更好地理解和应用GCD进行多线程开发。 ... [详细]
  • 并发编程 12—— 任务取消与关闭 之 shutdownNow 的局限性
    Java并发编程实践目录并发编程01——ThreadLocal并发编程02——ConcurrentHashMap并发编程03——阻塞队列和生产者-消费者模式并发编程04——闭锁Co ... [详细]
  • 深入解析Java多线程与并发库的应用:空中网实习生面试题详解
    本文详细探讨了Java多线程与并发库的高级应用,结合空中网在挑选实习生时的面试题目,深入分析了相关技术要点和实现细节。文章通过具体的代码示例展示了如何使用Semaphore和SynchronousQueue来管理线程同步和任务调度。 ... [详细]
  • 主调|大侠_重温C++ ... [详细]
  • 本文详细介绍了Java中实现异步调用的多种方式,包括线程创建、Future接口、CompletableFuture类以及Spring框架的@Async注解。通过代码示例和深入解析,帮助读者理解并掌握这些技术。 ... [详细]
  • 本文探讨了如何通过一系列技术手段提升Spring Boot项目的并发处理能力,解决生产环境中因慢请求导致的系统性能下降问题。 ... [详细]
  • BFS深搜hashtable来判断是横线还是竖线但是为啥还是90分啊呜呜!找不到原因#define_CRT_SECURE_NO_WARNINGS1#include ... [详细]
  • MainActivityimportandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;im ... [详细]
  • 在Swift项目中集成Objective-C类或第三方框架的方法
    本文通过实例讲解如何在Swift项目中引入并使用Objective-C编写的ProgressHUD库。首先需要在项目中添加库文件,并设置Objective-C桥接头文件以实现语言间的互操作性。 ... [详细]
  • 本文介绍了如何在 C# 和 XNA 框架中实现一个自定义的 3x3 矩阵类(MMatrix33),旨在深入理解矩阵运算及其应用场景。该类参考了 AS3 Starling 和其他相关资源,以确保算法的准确性和高效性。 ... [详细]
  • 由二叉树到贪心算法
    二叉树很重要树是数据结构中的重中之重,尤其以各类二叉树为学习的难点。单就面试而言,在 ... [详细]
  • Linux环境下进程间通信:深入解析信号机制
    本文详细探讨了Linux系统中信号的生命周期,从信号生成到处理函数执行完毕的全过程,并介绍了信号编程中的注意事项和常见应用实例。通过分析信号在进程中的注册、注销及处理过程,帮助读者理解如何高效利用信号进行进程间通信。 ... [详细]
  • 自 Swift 3.0 以来,Grand Central Dispatch (GCD) 的使用方式有了显著的变化,变得更加简洁高效。本文将介绍 GCD 的核心概念,包括队列、同步与异步操作、服务质量 (QoS) 以及常见的使用场景。 ... [详细]
  • 本文介绍了iOS应用开发的主要框架,包括Foundation、UIKit、CoreData及CoreGraphics等,并探讨了开发iOS应用所需的硬件和软件环境,以及推荐的编程语言。 ... [详细]
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社区 版权所有