我正在处理<ListView>
组件,我有一个名称列表,但是在更改状态后,它正在更改值,但它没有重新渲染 ListView。
isSelect:false - (状态)
isSelect:true - (状态)
代码:
var designerName = [{id: 'Calvin Klein', name: 'Calvin Klein', isSelected: false},
{id: 'Donatella Versace', name: 'Donatella Versace', isSelected: false},
{id: 'Valentino Garavani', name: 'Valentino Garavani', isSelected: false},
{id: 'Giorgio Armani', name: 'Giorgio Armani', isSelected: false}];
export default class filter extends Component {
constructor() {
super();
const listDs = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
});
this.state = {
listDs:designerName
};
}
componentDidMount(){
this.setState({
designerDataSource:this.state.designerDataSource.cloneWithRows(this.state.listDs),
})
}
render() {
return (
<View>
<ListView
dataSource={this.state.designerDataSource}
renderRow={this.renderRow.bind(this)}
/>
</View>
);
}
renderRow(item){
return (
<TouchableHighlight key={item.id} onPress={this.onItemDesigner.bind(this, item)}>
<View>
<View>
{this.renderName(item)}
</View>
<View>
<Text>{item.name}</Text>
</View>
</View>
</TouchableHighlight>
);
}
renderName(item){
if(item.isSelected) {
return(
<Image
style={{
width: 15,
height:15}}
source={require('../images/black_tick_mark.png')} />
);
}
}
onItemDesigner(item){
var tempDesigner = this.state.listDs.slice();
for(var i=0; i<tempDesigner.length; i++){
if (tempDesigner[i].id == item.id && !tempDesigner[i].isSelected) {
tempDesigner[i].isSelected = true;
}else if (tempDesigner[i].id == item.id && tempDesigner[i].isSelected){
tempDesigner[i].isSelected = false;
}
}
this.setState({
designerDataSource: this.state.designerDataSource.cloneWithRows(tempDesigner),
});
}
}
请仔细阅读我上面的代码,如果您找到任何解决方案,请告诉我。
谢谢