Search code examples
iosswiftuitableviewlag

UITableView lag when scroll


I use a tableView , manually cached cells , but when I scroll I get a little lag . This is my code :

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{


    var realRow = indexPath.row / 2
    if indexPath.row / 2 > self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
    {
        --realRow
    }
    if indexPath.row / 2 > self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
    {
        --realRow
    }

    if indexPath.row % 2 == 1
    {
        return self.CELL_MARGIN
    }
    else if indexPath.row / 2 == self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
    {
        return self.SUGGESTED_USERS_CELL_HEIGHT
    }
    else if indexPath.row / 2 == self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
    {
        return self.SUGGESTED_USERS_CELL_HEIGHT
    }
    else
    {
        return self.CELLS_HEIGHT[realRow]//[indexPath.row / 2 - indexPath.row / (self.SUGGESTED_USERS_RATE * 2 + 2)]
    }

// let row = indexPath.row / 2

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var realRow = indexPath.row / 2

    if indexPath.row / 2 > self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
    {
        --realRow
    }
    if indexPath.row / 2 > self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
    {
        --realRow
    }




    if indexPath.row % 2 == 1
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "")
        cell.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.3)

        cell.selectionStyle = .None

        addSeperatorsToView(cell.contentView, width: self.bounds.width,
            height: self.CELL_MARGIN, up: true, down: true, left: false, right: false)

        return cell
    }
    else if indexPath.row / 2 == self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
    {
        let cell = SuggestedUsersCell(width: self.bounds.width, height: self.SUGGESTED_USERS_CELL_HEIGHT, radios: suggestedRadioStations.prefix(5) + [])
        return cell
    }
    else if indexPath.row / 2 == self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
    {
        let cell = SuggestedUsersCell(width: self.bounds.width, height: self.SUGGESTED_USERS_CELL_HEIGHT, users: suggestedUsers.prefix(5)  + [])
        return cell
    }
    else
    {
        var cell : ActivityTableViewCell

        if let myCell = self.cells_cache[realRow]
        {
            cell = myCell
        }
        else
        {
            let activity = self.actevetiesArr[realRow]

            cell = ActivityTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.CELL_ID,
                height: self.CELLS_HEIGHT[realRow],
                width: self.bounds.width, activity: activity ,
                index : realRow  ,
                delegate : self)

            self.cells_cache[realRow] = cell

        }



        cell.userInteractionEnabled = true
        cell.selectionStyle = .None

        return cell
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    var count = self.actevetiesArr.count * 2

    if self.actevetiesArr.count > self.SUGGESTED_USERS_RATE
    {
        count = count + 2
    }
    if self.actevetiesArr.count > self.SUGGESTED_RADIOS_RATE
    {
        count = count + 2
    }

    if self.actevetiesArr.count < max(self.SUGGESTED_USERS_RATE , self.SUGGESTED_RADIOS_RATE)
    {
        count += 2
        self.SUGGESTED_RADIOS_RATE = 1
        self.SUGGESTED_USERS_RATE = 0
    }
    else
    {
        self.SUGGESTED_USERS_RATE = SUGGESTED_USERS_INDEX
        self.SUGGESTED_RADIOS_RATE = SUGGESTED_RADIOS_INDEX
    }

    if suggestedRadioStations.count == 0
    {
        count -= 2
    }

    if suggestedUsers.count == 0
    {
        count -= 2
    }

    return count//(self.actevetiesArr.count + (self.actevetiesArr.count 
}

what is the problem ??! , I don't do a huge amount of code in cellForRowAtIndexPath. Note : cell_cache is a dictionary ==> [Int: ActivityTableViewCell


Solution

  • add this

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return  44;
    }
    

    or you can add

    self.tableView. estimatedRowHeight = 44.0 in viewDidLoad

    let me know if it works