tableview怎样清空数据
时间: 2023-08-24 15:03:54 浏览: 67
清空 UITableView 的数据可以通过以下方法实现:
1. 清空数据源数组,然后调用 UITableView 的 reloadData 方法。
```swift
// Swift 代码
dataArray.removeAll()
tableView.reloadData()
```
```objective-c
// Objective-C 代码
[dataArray removeAllObjects];
[tableView reloadData];
```
2. 如果你用了数据源协议 `UITableViewDataSource` 中的 `numberOfRowsInSection` 方法,可以将其返回值设为 0,然后刷新 UITableView。
```swift
// Swift 代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
// 清空数据方法
dataArray.removeAll()
tableView.reloadData()
```
```objective-c
// Objective-C 代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dataArray.count;
}
// 清空数据方法
[dataArray removeAllObjects];
[tableView reloadData];
```