当我用十个或更少的元素填充ListView时,我可以选择触摸它的特定行并更改行背景颜色。但是,当我在ListView中放入更多的10个元素时,应用程序不会改变this.state.data。如果for循环中的i变量是> 10无法将状态更改为false / true,我无法更改行背景颜色...
有什么建议吗?
class UbicacionScreen extends React.Component {
constructor(props) {
super(props)
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
data: [],
dataSource: ds,
}
}
componentDidMount() {
var datas = [];
for (var i = 0; i <= 11; i++) {
datas.push({
row: i,
isSelect: false,
});
}
this.setState({data: datas})
this.setState({
dataSource: this.state.dataSource.cloneWithRows(datas)
})
}
render() {
return (
<ScrollView style={styles.body}>
<View style={styles.container}>
<View style={styles.listReglamentos}>
<List style={{backgroundColor: '#fff', marginTop: 14}}>
<ListView
renderRow={this._renderRow.bind(this)}
dataSource={this.state.dataSource}
/>
</List>
</View>
</View>
</ScrollView>
);
}
_renderRow(rowData: string, sectionID: number, rowID: number) {
return (
<TouchableHighlight onPress={() => { this._onPressRow(rowID, rowData) }}>
<View style={[rowData.isSelect ? styles.activeRow : styles.deactivedRow, styles.style_row_view]}>
<Text style={styles.style_text}>{rowData.municipio}</Text>
<Text style={styles.style_text}>{rowData.isSelect ? 'true' : 'false'}</Text>
</View>
</TouchableHighlight>
);
}
_onPressRow = (rowID, rowData) => {
rowData.isSelect = !rowData.isSelect;
var dataClone = this.state.data;
dataClone[rowID] = rowData;
this.setState({
data: dataClone
});
}
}
答案 0 :(得分:0)
不需要仅使用data
数组进行比较。您可以将所选值保存在状态中,然后更改此行
<View style={[
(rowData.isSelect)
? styles.activeRow
: styles.deactivedRow
, styles.style_row_view
]}>
到
<View style={[
(this.state.selectedValue == rowData.municipio)
? styles.activeRow
: styles.deactivedRow
, styles.style_row_view
]}>