自定义UITableviewCell滚动时缓慢和滞后

时间:2014-12-29 04:17:57

标签: ios performance uitableview swift

感谢您阅读我的问题。

我试过通过swift编程使自定义UITableViewCell不使用Buider接口(nib文件)

这是我的自定义UITableViewCell(ZHome_Post_TableViewCell_1)文件:

var titlePost:UILabel!
var btnCategory:UIButton!
var timePost:UILabel!
var thumbPost:UIImageView!
var customView:UIView!

var _data_category_name:NSString!
var _data_time_post:NSString!
var _data_title_post:NSString!
var _data_thumb_post:NSString!

let  screenWidth:CGFloat = UIScreen.mainScreen().bounds.width
let  screenHeight:CGFloat = UIScreen.mainScreen().bounds.height
let  screenBounds: CGRect = UIScreen.mainScreen().bounds
let  contentViewWidth:CGFloat = UIScreen.mainScreen().bounds.width - 15
let  row_height = _screen_width / 1.52


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    _data_category_name = dataHelper.catName
    _data_time_post = dataHelper.timePost
    _data_title_post = dataHelper.titlePost
    _data_thumb_post = dataHelper.urlThumb

    self.accessoryType = UITableViewCellAccessoryType.None
    self.selectionStyle = UITableViewCellSelectionStyle.None
    self.backgroundColor = UIColor(rgb: 0xf0efef)
    self.contentView.backgroundColor = UIColor.clearColor()

    if (customView == nil){
        customView = UIView(frame: CGRectMake(0, 0, contentViewWidth, row_height))
        customView.backgroundColor = UIColor.whiteColor()
        customView.layer.borderWidth = 0.6
        customView.layer.borderColor = UIColor(rgb: 0xd2d2d2).CGColor
        customView.layer.shadowColor = UIColor(rgb: 0xd2d2d2).CGColor
        customView.layer.shadowOffset = CGSizeMake(0.0, 1)
        customView.layer.shadowOpacity = 0.7
        customView.layer.cornerRadius = 1
        customView.layer.masksToBounds = true
        contentView.addSubview(customView)
    }

    if (thumbPost == nil){
        thumbPost = UIImageView(frame: CGRectMake(0, customView.frame.height / 3.0, customView.frame.width, row_height / 1.45))
        customView.addSubview(thumbPost)
    }


    if(titlePost == nil){
        titlePost = UILabel(frame: CGRectMake(7, 7, customView.frame.width - 10, row_height / 5.2))
        titlePost.font = UIFont(name: "HelveticaNeue-Medium", size: 14)
        titlePost.numberOfLines = 2
        titlePost.clipsToBounds = true
        titlePost.textColor = UIColor(rgb: 0x494949)
        titlePost.preferredMaxLayoutWidth = 60
        customView.addSubview(titlePost)

    }

    if(btnCategory == nil){
        btnCategory = SFlatButton(frame: CGRectMake(7, titlePost.frame.height + 5 , stringHeper.getSizeOfLabel(_data_category_name.uppercaseString).width + 10, 15), sfButtonType: SFlatButton.SFlatButtonType.SFBCategoryBlue)
        btnCategory.setTitle(_data_category_name.uppercaseString, forState: UIControlState.Normal)
        btnCategory.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 09)
        customView.addSubview(btnCategory)
    }

    if(timePost == nil){
        timePost = UILabel(frame: CGRectMake(btnCategory.frame.width + 15, titlePost.frame.height + 5, stringHeper.getSizeOfLabel(_data_time_post).width + 15, 15))
        timePost.text = _data_time_post
        timePost.textColor = UIColor(rgb: 0x9d9d9d)
        timePost.font = UIFont(name: "HelveticaNeue-Light", size: 09)
        customView.addSubview(timePost)

    }


}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

我初始化函数awakeFromNib()中的所有内容,每当我滚动UITableview时调用awakeFromNib,这会使应用程序变慢且滞后:( 那么,我怎样才能初始化控件一次。

谢谢!

更新:

我的cellForRowAtIndexPath:

    var _data_url_thumb : String = _dataArray[indexPath.row].objectForKey("thumbnail") as String;
    var _data_file_name : String = _dataArray[indexPath.row].objectForKey("fileName") as String;
    var _data_title:String = _dataArray[indexPath.row].objectForKey("title") as String;
    var _data_cat_name: NSString = _dataArray[indexPath.row].objectForKey("category_name") as String;
    var _data_author: NSString = _dataArray[indexPath.row].objectForKey("display_name") as String;
    var _data_published_at: NSString = _dataArray[indexPath.row].objectForKey("published_at") as String;
    var _data_avatar: NSString = _dataArray[indexPath.row].objectForKey("avatar") as String;

    dataHelper.gsCatName = _data_cat_name
    dataHelper.gsTimePost = _data_published_at
    dataHelper.gsTitlePost = _data_title
    dataHelper.gsUrlThumbPost = _data_url_thumb

    let cell = UITableViewCell()

    if (tableView == tableViewEvent){
        if(_dataEventsArray[0]["status"] as NSInteger == 0){

            let _data_event_at: String = _dataEventsArray[indexPath.row].objectForKey("event_at") as String
            let _data_event_title: String = _dataEventsArray[indexPath.row].objectForKey("title") as String
            let _data_event_post_id: String = _dataEventsArray[indexPath.row].objectForKey("post_id") as String

            dataHelper._gsEventTime = _data_event_at
            dataHelper._gsEventTitle = _data_event_title
            dataHelper.gsIdPost = _data_event_post_id

            var cell: ZHome_Event_TableViewCell = tableView.dequeueReusableCellWithIdentifier("home_event_custom_tableviewcell", forIndexPath: indexPath) as ZHome_Event_TableViewCell

            cell.awakeFromNib()
            return cell
        }

    }
    if (tableView == tableViewPost){

        if(indexPath.row == 4){
            var cell: ZHome_Post_TableViewCell_2 = tableView.dequeueReusableCellWithIdentifier("home_post_custom_tableviewcell_2", forIndexPath: indexPath) as ZHome_Post_TableViewCell_2
            cell.awakeFromNib()

            ImageHelper.sharedLoader.imageForUrl(_data_url_thumb, completionHandler:{(image: UIImage?, url: String) in
                cell.thumbPost.image = image
            })

            cell.titlePost.text = _data_title
            cell.titlePost.sizeToFit()

            return cell

        }else{


            var cell: ZHome_Post_TableViewCell_1 = tableView.dequeueReusableCellWithIdentifier("home_post_custom_tableviewcell_1", forIndexPath: indexPath) as ZHome_Post_TableViewCell_1
            cell.awakeFromNib()

            ImageHelper.sharedLoader.imageForUrl(_data_url_thumb, completionHandler:{(image: UIImage?, url: String) in
                cell.thumbPost.image = image
            })
            cell.titlePost.text = _data_title
            cell.titlePost.sizeToFit()

            return cell



        }

    }


    return cell

3 个答案:

答案 0 :(得分:2)

您没有重复使用tableview单元格。这将占用大量内存,因此您的代码可能会很慢。尝试重复使用单元格而不是每次都创建新单元格。

var cell = tableView.dequeueReusableCellWithIdentifier("YourCellName") as YourCellName!
if cell == nil {
    cell = YourCellName(style: UITableViewCellStyle.Default, reuseIdentifier: "YourCellName")
}

上述代码只会在必要时创建一个新代码。否则它将始终重复使用不可见的单元格。

希望这会有所帮助.. :)

答案 1 :(得分:0)

if(customView == nil

删除此if语句并继续。那条线导致问题

享受编码

答案 2 :(得分:0)

来自@Drongo的答案是正确的,但是第一次滚动tableview时滚动仍然是Laggy。

这是因为将调用方法awakeFromNib:并且实例化UITableViewCells,并使滚动延迟。但这是第一次,因为之后将重用这些细胞。