gpt4 book ai didi

ios - 我有一个 TableView 和五个数据源

转载 作者:行者123 更新时间:2023-11-29 00:17:15 25 4
gpt4 key购买 nike

我有一个表格 View 。有五个数据源。当我点击数据源切换按钮的时候,但是点击第一个按钮会是空的,其他按钮点击正常,一直没有找到原因。

这是代码:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray * dataArr;
@property(nonatomic,strong)NSMutableArray * dataArr0;
@property(nonatomic,strong)NSMutableArray * dataArr1;
@property(nonatomic,strong)NSMutableArray * dataArr2;
@property(nonatomic,strong)NSMutableArray * dataArr3;
@property(nonatomic,strong)NSMutableArray * dataArr4;

@property(nonatomic,strong)UITableView * mainTab;

@end


static NSString * cellID = @"cellID";

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray * mutarr0 = [NSMutableArray arrayWithObjects:@"all",@"all",@"all",@"all",@"all", nil];
self.dataArr0 = mutarr0;

NSMutableArray * mutarr1 = [NSMutableArray arrayWithObjects:@"Waiting",@"Waiting",@"Waiting",@"Waiting",@"Waiting",@"Waiting", nil];
self.dataArr1 = mutarr1;

NSMutableArray * mutarr2 = [NSMutableArray arrayWithObjects:@"processing",@"processing",@"processing",@"processing", nil];
self.dataArr2 = mutarr2;

NSMutableArray * mutarr3 = [NSMutableArray arrayWithObjects:@"End",@"End",@"End",@"End",@"End",@"End",@"End", nil];
self.dataArr3 = mutarr3;

NSMutableArray * mutarr4 = [NSMutableArray arrayWithObjects:@"cancel",@"cancel",@"cancel",@"cancel",@"cancel", nil];
self.dataArr4 = mutarr4;

self.dataArr = mutarr0;

NSArray * segArr = @[@"all",@"Waiting",@"processing",@"End",@"cancel"];

UISegmentedControl * segC = [[UISegmentedControl alloc]initWithItems:segArr];

segC.frame = CGRectMake(0,64, self.view.frame.size.width,50);

[segC addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

UITableView * tabVC = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(segC.frame),self.view.frame.size.width, self.view.frame.size.height - 50) style: UITableViewStylePlain];

tabVC.delegate = self;
tabVC.dataSource = self;
self.mainTab = tabVC;

[tabVC registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];

[self.view addSubview:segC];
[self.view addSubview:tabVC];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

cell.textLabel.text = self.dataArr[indexPath.row];

return cell;
}

-(void)change:(UISegmentedControl *)sender{
if (sender.selectedSegmentIndex == 0) {
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr0];

NSLog(@"%lu33333333333333",self.dataArr.count);
// NSLog(@"%lu44444444444444",self.dataArr0.count);
// //NSLog(@"%@-------%@",self.dataArr0[0],self.dataArr0[1]);
[self.mainTab reloadData];
}else if (sender.selectedSegmentIndex == 1){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr1];

NSLog(@"2");
[self.mainTab reloadData];
}else if (sender.selectedSegmentIndex == 2){
//
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr2];

[self.mainTab reloadData];
NSLog(@"3");
}else if (sender.selectedSegmentIndex == 3){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr3];

[self.mainTab reloadData];
// self.view.backgroundColor = [UIColor blueColor];
NSLog(@"4");
}else if (sender.selectedSegmentIndex == 4){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr4];

[self.mainTab reloadData];
// self.view.backgroundColor = [UIColor orangeColor];
NSLog(@"5");
}
}

@end

最佳答案

您的主要问题是在 viewDidLoad 中您执行以下操作:

self.dataArr = mutarr0;

然后在您的 change: 方法中执行以下操作:

[self.dataArr removeAllObjects];

这会删除 mutarr0(也是 self.dataArr0)中的所有值。

解决此问题的最佳方法是更改​​表单的所有行:

[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr4];

到:

self.dataArr = self.dataArr4;

就是这样。无需删除或添加任何对象。

有了这个工作,您还可以做出其他重大改进。主要是摆脱所有数组属性。您不需要 5 个单独的数组属性。只需创建一个代表数组的数组即可。

现在您的代码可以简化为:

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSMutableArray *current;
@property(nonatomic,strong) NSArray *data;

@property(nonatomic,strong)UITableView * mainTab;

@end


static NSString * cellID = @"cellID";

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *mutarr0 = [NSMutableArray arrayWithObjects:@"all",@"all",@"all",@"all",@"all", nil];
NSMutableArray *mutarr1 = [NSMutableArray arrayWithObjects:@"Waiting",@"Waiting",@"Waiting",@"Waiting",@"Waiting",@"Waiting", nil];
NSMutableArray *mutarr2 = [NSMutableArray arrayWithObjects:@"processing",@"processing",@"processing",@"processing", nil];
NSMutableArray *mutarr3 = [NSMutableArray arrayWithObjects:@"End",@"End",@"End",@"End",@"End",@"End",@"End", nil];
NSMutableArray *mutarr4 = [NSMutableArray arrayWithObjects:@"cancel",@"cancel",@"cancel",@"cancel",@"cancel", nil];

self.data = @[ mutarr0, mutarr1, mutarr2, mutarr3, mutarr4 ];
self.current = self.data[0];

NSArray * egArr = @[ @"all", @"Waiting", @"processing", @"End", @"cancel" ];

UISegmentedControl * segC = [[UISegmentedControl alloc]initWithItems:segArr];

segC.frame = CGRectMake(0,64, self.view.frame.size.width,50);

[segC addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

UITableView * tabVC = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(segC.frame),self.view.frame.size.width, self.view.frame.size.height - 50) style: UITableViewStylePlain];

tabVC.delegate = self;
tabVC.dataSource = self;
self.mainTab = tabVC;

[tabVC registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];

[self.view addSubview:segC];
[self.view addSubview:tabVC];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.current.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

cell.textLabel.text = self.current[indexPath.row];

return cell;
}

-(void)change:(UISegmentedControl *)sender{
if (sender.selectedSegmentIndex != UISegmentedControlNoSegment {
self.current = self.data[sender.selectedSegmentIndex];
[self.mainTab reloadData];
}
}

@end

关于ios - 我有一个 TableView 和五个数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45046886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com