【发布时间】:2016-11-12 02:06:54
【问题描述】:
我正在开发一个 react native 应用程序,我试图让一些图像像一个按钮一样,这样当你按下它们时,它们会在控制台中打印一条语句。
我的代码是:
class ImageList extends Component {
componentWillMount(){
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(this.props.images);
}
imageTouched(){
console.log('pressed');
}
renderRow(rowData){
const {uri} = rowData;
return(
<View style={{padding: 1, alignSelf: 'flex-start'}}>
<TouchableHighlight onPress={this.imageTouched}>
<Image style={styles.imageSize} source={{uri: uri}} />
</TouchableHighlight>
</View>
)
}
render() {
return (
<View>
<ListView
contentContainerStyle={styles.list}
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
</View>
);
}
}
var styles = StyleSheet.create({
imageSize: {
//newWidth is the width of the device divided by 4.
//This is so that four images will display in each row.
width: newWidth,
height: newWidth,
padding: 2
},
list: {
flexDirection: 'row',
flexWrap: 'wrap'
}
});
当我运行它时没有错误,但是当我触摸图像时没有任何反应。我检查了控制台,但没有打印任何内容。
如何让每张图片都充当按钮?
【问题讨论】:
-
如果将
this.imageTouched替换为函数中的console.log() 语句,是否可以记录?我相信当您调用 imageTouched() 函数时,您的作用域是错误的。 This post 应该有助于澄清。
标签: image button react-native