Mohsen Shakiba
Mohsen Shakiba

Reputation: 1872

swift - rendering table view is slow

I'm have a table view and I'm using the tableView.dequeueReusableCellWithIdentifier to reuse the cells but still tableView is very slow.
and by slow, I mean it takes about 500 milliseconds to put 9 of my views in the tableView. and it's tested on apple A7 X64 processor so it must be pretty slower on older processors. the reason that it's slow is because there are a few sub views and constraints.
but I've seen more complex tableCells with better performance, so there must be something I can do.
like caching a cell or something else??
any ideas?

sample code

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        tableView.registerNib(UINib(nibName: "ChatCell", bundle: nil), forCellReuseIdentifier: "ChatCell")
        let cell = tableView.dequeueReusableCellWithIdentifier("ChatCell") as! ChatCell
        return cell
}

Upvotes: 2

Views: 3009

Answers (3)

fishinear
fishinear

Reputation: 6346

The call to registerNib is normally done only once in viewDidLoad, not every time you are asked for a cell in cellForRowAtIndexPath. Not sure how slow that call is, but it might be the reason for your slow response.

Upvotes: 1

Imran Sh
Imran Sh

Reputation: 1774

I think you are using effects (like shadow or round corners or etc) or having heavy calculations on UI

Edit: Code Sample added

//Add in your init func
tblView.registerClass(MSCustomVerticalListCell.self, forCellReuseIdentifier: NSStringFromClass(MSCustomVerticalListCell))

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tblView.dequeueReusableCellWithIdentifier(NSStringFromClass(MSCustomVerticalListCell), forIndexPath: indexPath) as! MSCustomVerticalListCell
     //add data binding
     cell.item = dataSource[indexPath.row]
     return cell
}

Your data binding class (Data Model):

class MSC_VCItem
{
    var Title:String!
    var Action:String!
    var SubTitle:String!
    var Icon:String!

    init(title:String!,subTitle:String!,icon:String!,action:String!)
    {
        self.Title = title
        self.SubTitle = subTitle
        self.Icon = icon
        self.Action = action
    }
}

And Finally you custom table cell:

class MSCustomVerticalListCell : UITableViewCell {
    let padding = 5
    let imageWidth = 50
    var customImageView: UIImageView!
    var customTitleLabel: UILabel!
    var customSubtitleLabel: UILabel!
    var item: MSC_VCItem? {
        didSet {
            if let it = item {
                customTitleLabel.text = it.Title
                customSubtitleLabel.text = it.SubTitle
                UIImage.loadFromCacheOrURL(it.Icon, callback: { (image: UIImage) -> () in
                    self.customImageView.image = image
                })
                setNeedsLayout()
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clearColor()

        customTitleLabel = UILabel(frame: CGRectZero)
        self.addSubview(customTitleLabel)

        customSubtitleLabel = UILabel(frame: CGRectZero)
        contentView.addSubview(customSubtitleLabel)

        customImageView = UIImageView(frame: CGRectZero)
        customImageView.image = UIImage(named: "default")
        contentView.addSubview(customImageView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        //Write your UI here like bg color or text color
    }
}

Upvotes: 0

Kjuly
Kjuly

Reputation: 35181

the reason that it's slow is because there are a few sub views and constraints.

Personally, I don't suggest you use constraints in cell, especially when there're many subviews, it'll cost much CPU time and lead the scrolling lag. Instead, you can calculate manually based on cell frame.

And for more suggestion, i suggest you take time to read this post: Simple Strategies for Smooth Animation on the iPhone.

Upvotes: 3

Related Questions