【发布时间】:2016-04-28 15:55:57
【问题描述】:
我可以将静态图像添加到ListView 单元格中(参见下面的代码),但是如何动态更改图标图像?
<Image source={require('./img/check.png')} />
是为 iOS 和 Android 引用图像文件的推荐方式。
我有一个名为 ExpandingCell 的组件,它可以选择显示一堆不同的图标,但其他一切都保持不变。
在ListView 中,我创建了一个cell 对象,然后将其传递给ExpandingCell 进行渲染
ListView 数据源数组如下所示:
var LIST_DATA = [
...
{type: 'ExpandingCell',
icon: './CellIcons/MagnifyingGlassIcon.png', //unused
title: 'Lorem Ipsum',
detail: 'Donec id elit non mi porta gravida at eget metus.'},
...
];
然后在renderCell 方法中传递上述单元格对象:
renderCell(cell) {
if (cell.type === 'ExpandingCell') {
return (
<ExpandingCell cell={cell} />
);
}
}
现在在ExpandingCell 我为render() 准备了这个:
render() {
return (
...
<Image source{
require('./CellIcons/MagnifyingGlassIcon.png')
}>
</Image>
...
);
}
但是,当我尝试像这样使用this.props.cell.icon 时:
<Image source={require(this.props.cell.icon)}></Image>
我收到以下错误:Requiring unknown module "./CellIcons/MagnifyingGlassIcon.png".
提前致谢 =)
【问题讨论】:
标签: image reactjs react-native