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

时间:2017-07-12 01:32:53

标签: ios objective-c uitableview

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

这是代码:

#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 个答案:

答案 0 :(得分:1)

您的主要问题是在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

答案 1 :(得分:0)

您不需要更改代码结构,但我同意可以更有效地编写代码结构。但主要问题在于这一行:

self.dataArr = mutarr0;

你的self.dataArr从未被初始化为独立财产。只需用这些替换该行,它应该可以工作:

self.dataArr = [[NSMutableArray alloc] init]; // initialize it first!
[self.dataArr addObjectsFromArray:mutarr0]; // and then use it

您还应该初始化所有其他属性, dataArr0-dataArr5。

答案 2 :(得分:0)

问题是。

self.dataArr = mutarr0;

self.dataArr将指向mutarr0self.dataArr没有自己的记忆。你可以看到 this point

删除时

if (sender.selectedSegmentIndex == 0) {
    [self.dataArr removeAllObjects];
    ...
}
删除了

mutarr0个对象,因此您应该为self.dataArr创建内存。楼上给出了很好的解决方案。