在React Native中删除ListView项
我很难从列表视图中删除一行。它将删除列表的最后一个元素,无论单击哪个但是,console.log告诉我,数组已正确删除该项,但渲染错误…internet上已有一些解决方案,但没有一个能帮助我解决问题 日志 如您所见,它实际上删除了正确的项,但显示了错误的数组在React Native中删除ListView项,listview,react-native,react-native-listview,Listview,React Native,React Native Listview,我很难从列表视图中删除一行。它将删除列表的最后一个元素,无论单击哪个但是,console.log告诉我,数组已正确删除该项,但渲染错误…internet上已有一些解决方案,但没有一个能帮助我解决问题 日志 如您所见,它实际上删除了正确的项,但显示了错误的数组 11月6日22:18:39维维安空中提醒[25992]:{rowID:'0'} 11月6日22:18:39 Viviennes空中提醒[25992]:[“测试2”,“测试3”] 11月6日22:18:39 Viviennes空中提醒[25
11月6日22:18:39维维安空中提醒[25992]:{rowID:'0'}
11月6日22:18:39 Viviennes空中提醒[25992]:[“测试2”,“测试3”]
11月6日22:18:39 Viviennes空中提醒[25992]:{u rowHasChanged:[功能:rowHasChanged],
_getRowData:[函数:defaultGetRowData],
_sectionHeaderHasChanged:[函数],
_getSectionHeaderData:[函数:defaultGetSectionHeaderData],
_dataBlob:{s1:['测试2','测试3']},
_dirtyRows:[[false,false,true]],
_dirtySections:[假],
_缓存计数:3,
行标识:['0','1','2']],
SectionIdentifies:['s1']}
这是我的构造函数:
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
items: [],
dataSource: ds.cloneWithRows([]),
newItem: "",
};}
列表视图:
<ListView
style={styles.container}
dataSource={this.state.dataSource}
enableEmptySections={true}
renderRow={(data, sectionID, rowID) => this.renderRow(data, sectionID, rowID)}
renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator}/>}
/>
我已经花了两个小时试图解决这个问题,我很确定我做的每件事都是正确的。我同意Burak Karasoy的观点,但代码中还有一个错误
var newList= this.state.items.splice(index, 1)
newList将包含已删除的项目,但实际上我们需要的是删除项目后的新列表
因此,使代码正常工作所需的更改是
不要在状态对象中存储项。而是将其存储为类实例变量,并设置数据源
,如下面的代码所示
this.items=this.getItems();
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.items)
});
现在更新您的\u delete
方法,如下所示
_delete(index) {
this.items.splice(index, 1)// This will remove the element at index, and update this.items with new array
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.items)
});
}
谢谢解决了我的问题。
this.items=this.getItems();
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.items)
});
_delete(index) {
this.items.splice(index, 1)// This will remove the element at index, and update this.items with new array
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.items)
});
}