I have a table view. There are five data sources. When I click on the button when the data source switch, but click the first button will be empty, the other button click normal, has not found the reason.
我有一个表视图。有五个数据源。当我点击按钮的时候数据源切换,但点击第一个按钮就会为空,其他按钮点击正常,一直没找到原因。
This is the code:
这是代码:
#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
3 个解决方案
#1
1
Your primary problem is that in viewDidLoad
you do:
你的主要问题是在viewDidLoad中你做了:
self.dataArr = mutarr0;
And then in your change:
method you do:
然后在你的改变:你做的方法:
[self.dataArr removeAllObjects];
This erases all of the values from mutarr0
(which is also self.dataArr0
).
这将删除mutarr0中的所有值(也是self.dataArr0)。
The best way to fix this is to change all lines of the form:
解决此问题的最佳方法是更改表单的所有行:
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr4];
to:
至:
self.dataArr = self.dataArr4;
That's it. No need to remove or add any objects.
而已。无需删除或添加任何对象。
With that working, there are other big improvements you can make. Mainly, get rid of all of the array properties. You don't need 5 separate array properties. Just create one that represents an array of your arrays.
通过这项工作,您可以做出其他重大改进。主要是,摆脱所有的数组属性。您不需要5个单独的数组属性。只需创建一个代表数组数组的代码。
Now your code can be simplified to just:
现在您的代码可以简化为:
@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
#2
0
You don't need to change your code structure, though I agree it could be written more efficiently. But the main problem is in this line:
您不需要更改代码结构,但我同意它可以更有效地编写。但主要问题在于这一行:
self.dataArr = mutarr0;
You self.dataArr was NEVER initialized as an independent property. Just replace that line with these and it should work:
你self.dataArr从未被初始化为独立财产。只需用这些替换该行,它应该工作:
self.dataArr = [[NSMutableArray alloc] init]; // initialize it first!
[self.dataArr addObjectsFromArray:mutarr0]; // and then use it
You should also initialize all the other properties, dataArr0-dataArr5.
您还应该初始化所有其他属性dataArr0-dataArr5。
#3
0
The problem is.
问题是。
self.dataArr = mutarr0;
self.dataArr
will point to mutarr0
, self.dataArr
does not have its own memory. You can see this point
self.dataArr将指向mutarr0,self.dataArr没有自己的内存。你可以看到这一点
When you remove
当你删除
if (sender.selectedSegmentIndex == 0) {
[self.dataArr removeAllObjects];
...
}
mutarr0
objects is removed , so you should create the memory for self.dataArr
. Upstairs gave a good resolution.
删除mutarr0对象,因此您应该为self.dataArr创建内存。楼上给出了很好的解决方案。
#1
1
Your primary problem is that in viewDidLoad
you do:
你的主要问题是在viewDidLoad中你做了:
self.dataArr = mutarr0;
And then in your change:
method you do:
然后在你的改变:你做的方法:
[self.dataArr removeAllObjects];
This erases all of the values from mutarr0
(which is also self.dataArr0
).
这将删除mutarr0中的所有值(也是self.dataArr0)。
The best way to fix this is to change all lines of the form:
解决此问题的最佳方法是更改表单的所有行:
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr4];
to:
至:
self.dataArr = self.dataArr4;
That's it. No need to remove or add any objects.
而已。无需删除或添加任何对象。
With that working, there are other big improvements you can make. Mainly, get rid of all of the array properties. You don't need 5 separate array properties. Just create one that represents an array of your arrays.
通过这项工作,您可以做出其他重大改进。主要是,摆脱所有的数组属性。您不需要5个单独的数组属性。只需创建一个代表数组数组的代码。
Now your code can be simplified to just:
现在您的代码可以简化为:
@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
#2
0
You don't need to change your code structure, though I agree it could be written more efficiently. But the main problem is in this line:
您不需要更改代码结构,但我同意它可以更有效地编写。但主要问题在于这一行:
self.dataArr = mutarr0;
You self.dataArr was NEVER initialized as an independent property. Just replace that line with these and it should work:
你self.dataArr从未被初始化为独立财产。只需用这些替换该行,它应该工作:
self.dataArr = [[NSMutableArray alloc] init]; // initialize it first!
[self.dataArr addObjectsFromArray:mutarr0]; // and then use it
You should also initialize all the other properties, dataArr0-dataArr5.
您还应该初始化所有其他属性dataArr0-dataArr5。
#3
0
The problem is.
问题是。
self.dataArr = mutarr0;
self.dataArr
will point to mutarr0
, self.dataArr
does not have its own memory. You can see this point
self.dataArr将指向mutarr0,self.dataArr没有自己的内存。你可以看到这一点
When you remove
当你删除
if (sender.selectedSegmentIndex == 0) {
[self.dataArr removeAllObjects];
...
}
mutarr0
objects is removed , so you should create the memory for self.dataArr
. Upstairs gave a good resolution.
删除mutarr0对象,因此您应该为self.dataArr创建内存。楼上给出了很好的解决方案。