Initial lag for a cell with a UITextView

Current setting

I have a custom cell loaded from xib where most of its space is covered by a UITextview. Also this cell can have several text views. And in this cell there are several more elements (one UIView + 2 UILabels).

Problem

I tried to remove all of these views and delays, even if I only have one text view. In addition, the lag occurs only for the first time. Later, when I scroll down and run to another cell with the text in it, the lag does not occur at all.

Additional Information

The thing with this custom cell is that the text view is added to UIStackView. At the beginning, stackview is empty because I do not know (at development time) how many text views can / should be there.

I know that this is another thing that can affect performance, but I solved it (I think it is the best way possible) by checking how many text views were already found in the stackview array arrangedSubviewswhen the cell was deleted, and based on this information I just add or I hide the views appropriately (instead of destroying and re-creating the required number of text views each time).

I tried using the "Tools", but I did not notice that any of my classes took CPU time, but some method calls UIKitcaused by the infrastructure inside are the reason for this ... If necessary, I can post a screenshot, but I think that this is not relevant, because they seem like ordinary system and infrastructure calls. Plus I'm testing the iPad 2: D, so maybe this is the thing (I need to optimize the app for slow devices).

However, I think I can somehow optimize it?

The MyCell class is pretty simple (pseudo code):

class MyCell:UITableViewCell{

    func configure(data:SomeData){

        self.addOrHideViewsIfNeeded()
    }

    private func addOrHideViewsIfNeeded(){
        //here, I check if stackview.arrangedSubviews has, and how many subviews are there, and
        //add / hide them appropriately, means if I have to add them, I load them from the nib, otherwise, I reuse views from by adding them/removing them from a pool.
    }
}

In addition, the lag is more noticeable in the Debug version compared to the Release version, which makes sense, but it is still noticeable.

+4
2

, , .

class MyCell : UITableViewCell {
    static var initiallyPreloaded : Bool = false

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        if !MyCell.initiallyPreloaded {
//Do the initial preloading setup by adding UITextView to self.contentView
            MyCell.initiallyPreloaded = true
        } else {
//Setup the regular cell content otherwise
        }
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class ViewController: UIViewController , UITableViewDelegate {
    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        var cell: MyCell? = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier") as! MyCell?
        if cell == nil {
            cell = MyCell(style: .default, reuseIdentifier: "myCellIdentifier")
        }
        if indexPath.row == 0 {
//Display the initial cell in unnoticeable to user way (start with hidden/alpha zero)
//For this zero-th row the initial preloading should happen
        }
        return cell!
    }
}
+1

, , .

, :

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NAIndexPath *) indexPath{

static NSString *cellIdentifier = @"Mycell";

cell = [tableView dequeueCellWithIdentifier:cellIdentifier];

if(cell == nil)
    cell = [[MyCell alloc] initWithStyle: UITableViewCellStyleDefault    reuseIdentifier: cellIdentifier];
 }
+2

Source: https://habr.com/ru/post/1672926/


All Articles