0

我们有一个示例应用程序,它从数据库中加载 10 条记录,并在表格视图中显示。接下来的十条记录是在滚动到达底部时获取的(如厨房水槽中的动态滚动视图示例并遵循相同的示例代码)。然而,当记录数增加时应用程序滚动变得越来越慢,然后当我们显示接近第 1000 个记录时崩溃。我们还有更多记录要显示(10000),所有行都显示一个 50X50 的图像和两个文本。

if (search.value != null && search.value != ''){
    dbrows = db.execute('select id, name, scientificname from siteRecords where name            like \'%' + search.value + '%\' order by commonname limit 10 offset ' + lastRow);
}else{
    dbrows = db.execute('select id, name, scientificname from siteRecords order by name    limit 10 offset ' + lastRow);
}

tsEnd = new Date;
var duration = tsBegin.getTime() - tsEnd.getTime();
perfTableView.appendRow({title:"To fetch the record from 0 to " + (lastRow + 20) + " took: " + duration + " ms"});

tsBegin = new Date;
var rowCount = 0;
while (dbrows.isValidRow()) {
    var row;

    if( dbrows.fieldByName('name')[0] != curheader || initHeader == 0){
        initHeader = 1;
        curheader = dbrows.fieldByName('name')[0];
        row = Ti.UI.createTableViewRow({height:55,backgroundColor:'#ffffff',backgroundSelectedColor:'#eeee33',hasChild:true,className:'birds',header:curheader});
        index.push({title:curheader,index:rowNumber });
    } else {
        row = Ti.UI.createTableViewRow({height:55,backgroundColor:'#ffffff',backgroundSelectedColor:'#eeee33',hasChild:true,className:'birds'});
    }

    var lblBirdID = Ti.UI.createLabel({
        text: dbrows.fieldByName('id'),
        color: '#000000',
        textAlign:'left',
        left:4,
        top:8,
        height:100,
        font:{fontWeight:'bold',fontSize:10},
        visible:false
    });
    row.add(lblBirdID);

    media = dbrows.fieldByName('scientificname').replace(' ', '_') + '.jpg';
    var path = Titanium.Filesystem.resourcesDirectory + 'Birds/images/'

    if (Titanium.Filesystem.getFile(path,media).exists())
    {
        var f;

        if (isAndroid){
            f = '../images/' + media;
        }else{
            f = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'Birds/images/' + media);
        }

        var imgBird = Ti.UI.createImageView({
            image:f,
            left:4,
            height:50,
            width:50
        });
        row.add(imgBird);
    }else{
        var f;

        if (isAndroid){
            f = '../images/no_bird.jpg';
        }else{
            f = f = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'Birds/images/no_bird.jpg');
        }

        var imgBird = Ti.UI.createImageView({
            image:f,
            left:4,
            height:50,
            width:50
        });
        row.add(imgBird);
    }

    var lblBirdName = Ti.UI.createLabel({
        text: dbrows.fieldByName('name'),
        color: '#000000',
        textAlign:'left',
        left:60,
        top:8,
        height:20,
        font:{fontWeight:'bold',fontSize:16}
    });
    row.add(lblBirdName);

    var lblBirdScientificName = Ti.UI.createLabel({
        text: dbrows.fieldByName('scientificname'),
        color: '#000000',
        textAlign:'left',
        left:60,
        top:26,
        height:20,
        font:{fontSize:11}
    });
    row.add(lblBirdScientificName);

    birdRows[rowNumber] = row;
    rowCount = rowCount + 1;
    rowNumber = rowNumber + 1;
    dbrows.next();
}
dbrows.close();
db.close();

if (rowCount == 20){
    lastRow = lastRow + 20;
}else{
    lastRow = lastRow + rowCount;
}

birdTableView.setData(birdRows);
birdTableView.index = index;

tsEnd = new Date;
duration = tsBegin.getTime() - tsEnd.getTime();
perfTableView.appendRow({title:"To loop through the DB rows and to create table rows took: " + duration + " ms"});

}

4

1 回答 1

2

您是否尝试过在顶部卸载项目,因为项目被添加到底部(反之亦然)?做这样的事情意味着任何时候都不会超过 100 行。

此外,如果可能,请使用标准行而不是自定义行。标准行比自定义行性能要好得多。

于 2011-07-06T13:52:27.277 回答