Ios 我有一个表视图和五个数据源

Ios 我有一个表视图和五个数据源,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一张桌子。有五个数据来源。当我点击按钮切换数据源时,但点击第一个按钮将为空,另一个按钮点击正常,未找到原因 代码如下: #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong)NSMutableArray * dataArr; @property(nonatomic,strong)N

我有一张桌子。有五个数据来源。当我点击按钮切换数据源时,但点击第一个按钮将为空,另一个按钮点击正常,未找到原因

代码如下:

#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
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)NSMutableArray*dataArr;
@属性(非原子,强)NSMutableArray*dataArr0;
@属性(非原子,强)NSMutableArray*dataArr1;
@属性(非原子,强)NSMutableArray*dataArr2;
@属性(非原子,强)NSMutableArray*dataArr3;
@属性(非原子,强)NSMutableArray*dataArr4;
@属性(非原子,强)UITableView*Main选项卡;
@结束
静态NSString*cellID=@“cellID”;
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
NSMUTABLEARRY*mutarr0=[NSMUTABLEARRY数组,其中包含以下对象:@“all”、“all”、“all”、“all”、“nil];
self.dataArr0=mutarr0;
NSMUTABLEARRY*mutarr1=[NSMUTABLEARRY数组,其对象为:@“Waiting”、@“Waiting”、@“Waiting”、@“Waiting”、@“Waiting”、nil];
self.dataArr1=mutarr1;
NSMUTABLEARRY*mutarr2=[NSMUTABLEARRY数组,其对象为:@“正在处理”,“正在处理”,“正在处理”,无];
self.dataArr2=mutarr2;
NSMUTABLEARRY*mutarr3=[NSMUTABLEARRY数组,其对象为:@“End”、@“End”、@“End”、@“End”、@“End”、@“End”、nil];
self.dataArr3=mutarr3;
NSMUTABLEARRY*mutarr4=[NSMUTABLEARRY数组,其对象为:@“取消”,“取消”,“取消”,“取消”,无];
self.dataArr4=mutarr4;
self.dataArr=mutarr0;
NSArray*segArr=@[@“全部”、@“等待”、@“处理”、@“结束”、@“取消”];
UISegmentedControl*segC=[[UISegmentedControl alloc]initWithItems:segArr];
segC.frame=CGRectMake(0,64,self.view.frame.size.width,50);
[segC addTarget:self action:@selector(更改:)for ControlEvents:UIControlEventValueChanged];
UITableView*tabVC=[[UITableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(segC.frame),self.view.frame.size.width,self.view.frame.size.height-50)样式:UITableViewStylePlain];
tabVC.delegate=self;
tabVC.dataSource=self;
self.mainTab=tabVC;
[tabVC registerClass:[UITableViewCell类]强制重用标识符:cellID];
[self.view addSubview:segC];
[self.view addSubview:tabVC];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.textlab.text=self.dataArr[indexPath.row];
返回单元;
}
-(无效)更改:(UISegmentedControl*)发送方{
如果(sender.selectedSegmentIndex==0){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr0];
NSLog(@“%lu33333”,自数据阵列计数);
//NSLog(@“%lu4444”,self.dataArr0.count);
////NSLog(@“%@------%@”,self.dataArr0[0],self.dataArr0[1]);
[self.mainTab重新加载数据];
}else if(sender.selectedSegmentIndex==1){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr1];
NSLog(@“2”);
[self.mainTab重新加载数据];
}else if(sender.selectedSegmentIndex==2){
//
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr2];
[self.mainTab重新加载数据];
NSLog(@“3”);
}else if(sender.selectedSegmentIndex==3){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr3];
[self.mainTab重新加载数据];
//self.view.backgroundColor=[UIColor blueColor];
NSLog(@“4”);
}else if(sender.selectedSegmentIndex==4){
[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray:self.dataArr4];
[self.mainTab重新加载数据];
//self.view.backgroundColor=[UIColor orangeColor];
NSLog(@“5”);
}
}
@结束

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

self.dataArr = mutarr0;
然后在您的
更改:
方法中执行以下操作:

[self.dataArr removeAllObjects];
这将删除
mutarr0
(也是
self.dataArr0
)中的所有值

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

[self.dataArr removeAllObjects];
[self.dataArr addObjectsFromArray: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
@界面视图控制器()
@属性(非原子,强)NSMutableArray*当前;
@属性(非原子、强)NSArray*数据;
@属性(非原子,强)UITableView*Main选项卡;
@结束
静态NSString*cellID=@“cellID”;
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
NSMUTABLEARRY*mutarr0=[NSMUTABLEARRY数组,其中包含以下对象:@“all”、“all”、“all”、“all”、“nil];
NSMUTABLEARRY*mutarr1=[NSMUTABLEARRY数组,其对象为:@“Waiting”、@“Waiting”、@“Waiting”、@“Waiting”、@“Waiting”、nil];
NSMUTABLEARRY*mutarr2=[NSMUTABLEARRY数组,其对象为:@“正在处理”,“正在处理”,“正在处理”,无];
NSMUTABLEARRY*mutarr3=[NSMUTABLEARRY数组,其对象为:@“End”、@“End”、@“End”、@“End”、@“End”、@“End”、nil];
NSMUTABLEARRY*mutarr4=[NSMUTABLEARRY数组,其对象为:@“取消”,“取消”,“取消”,“取消”,无];
self.data=@[mutarr0,mutarr1,mutarr2,mutarr3,mutarr4];
self.current=self.data[0];
NSArray*egArr=@[@“全部”、@“等待”、@“处理”、@“结束”、@“取消”];
UISegmentedControl*segC=[[UISegmentedControl alloc]initWithItems:segArr];
segC.fr
@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
self.dataArr = mutarr0;
self.dataArr = [[NSMutableArray alloc] init]; // initialize it first!
[self.dataArr addObjectsFromArray:mutarr0]; // and then use it
self.dataArr = mutarr0;
if (sender.selectedSegmentIndex == 0) {
    [self.dataArr removeAllObjects];
    ...
}