如何在分段控件更改后重新加载UITableView?
问题描述:
我想控制tableview与分段控制(这是上面的tableview)的上下文。在更改段我想重新加载我的表tableView与不同的单元格(我有两种类型的单元格和每种类型的数据数组)的数据。如何清除并重新加载不同单元格的数据?如何在分段控件更改后重新加载UITableView?
答
我只是提供了几行代码,以@ jaydee3提到的。
- (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender
{
//Do something
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.segmentControl.selectedSegmentIndex?[self.dataArray2 count]:[self.dataArray1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.segmentControl.selectedSegmentIndex){
// Create Type cell for selected Index 1
}
else{
// Create Type cell for selected Index 0
}
}
答
在您的tableView delegate
中,检查分段控件中的选定内容并返回正确的单元格。
如果分段控制得到更改,请致电reloadData
登录tableView
。
答
1.如果您想要更改2个或更多tableview中的数据,那么最重要的一行是低。
@IBAction func segmentValueChanged(_ sender: UISegmentedControl)
{
self.<YourTableViewName>.reloadData()
}
2.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var index = <segmentObjectCreated>.selectedSegmentIndex
if index == 0 {
return <table1ArrayList>.count
}
else{
return <table12ArrayList>.count
}
}
3.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = <TableViewObj>.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var index = <segmentObjectCreated>.selectedSegmentIndex
if index == 0 {
cell.textLabel?.text = <table1ArrayList>[indexPath.row].productName
}
else {
cell.textLabel?.text = <table2ArrayList>[indexPath.row].newLaunch
}
return cell
}
就是这样,编码快乐!