我想知道 Ti.UI.TableViewRow 的“className”属性在创建自定义行时有何帮助。
例如,我通过以下方式使用自定义行填充表格 View :
function populateTableView(tableView, data) {
var rows = [];
var row;
var title, image;
var i;
for (i = 0; i < data.length; i++) {
title = Ti.UI.createLabel({
text : data[i].title,
width : 100,
height: 30,
top: 5,
left: 25
});
image = Ti.UI.createImage({
image : 'some_image.png',
width: 30,
height: 30,
top: 5,
left: 5
});
/* and, like, 5+ more views or whatever */
row = Ti.UI.createTableViewRow();
row.add(titleLabel);
row.add(image);
rows.push(row);
}
tableView.setData(rows);
}
当然,使用 TableViewRow 的标准 title
和 image
属性可以轻松创建这个“自定义”行示例,但这不是重点。如何防止 TableView 的新标签、 ImageView 和其他 subview 的分配以利于它们的重用?我知道在 iOS 中,这是通过使用方法 -[UITableView dequeueReusableCellWithIdentifier:]
从当前不是的“reservoir”(因此“className”在这里是“identifier”)获取行对象来实现的用于显示数据,但已经在其中正确布局了所需的 subview ,因此只需要更新其中包含的数据(文本、图像数据等)。
由于这个系统非常简单,我很难相信 Titanium API 使用的方法不支持这一点。
在阅读 API 并搜索网络后,我确实怀疑情况确实如此。建议使用“className”属性作为在 Titanium 中提高 TableView 效率的简单方法,但它与自定义 TableView 行的关系没有以任何方式解释。如果有人能为我澄清这个问题,我将非常感激。
最佳答案
Titanium SDK 使用 -[UITableView dequeueReusableCellWithIdentifier:]
来重用单元格布局。在 UITableView 代理(Titanium SDK 2.1.3)中,您有以下内容:
TiUITableView.m(第 1720 行)
- (UITableViewCell *)tableView:(UITableView *)ourTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UITableViewCell *cell = [ourTableView dequeueReusableCellWithIdentifier:row.tableClass];
...
}
此代码用于重用布局单元。这个方法的更多内容可以查看here ,在 Apple 文档中。
关于带有自定义行的 Titanium TableViewRow 类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969535/