0

我的应用程序添加了一个滚动视图和一个表视图作为子视图。在滚动视图上我有两个按钮。我想在按下这些按钮时在我的表格视图中加载两个单独的数组。但由于某种原因,它不会发生。谁能帮助我我做错了什么?

- (void)viewDidLoad
{
    [super viewDidLoad];
worldArray=[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",nil];
IndiaArray=[[NSMutableArray alloc]initWithObjects:@"abc",@"pqr",@"xyz", nil];
 [self PutButtoninScrollView:2];
}

-(void)PutButtoninScrollView:(int)numberOfbuttons
{
    myButton1=[[UIButton alloc]init];
    myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
    myButton1.frame=CGRectMake(5, 5, 70, 30);
    [myButton1 setTitle:@"World" forState:UIControlStateNormal];
    [myButton1 setTag:1];
    myButton1.backgroundColor=[UIColor blackColor];
    [myButton1 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:myButton1];

    myButton2=[[UIButton alloc]init];
    myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton2.frame=CGRectMake(80, 5, 70, 30);
    [myButton2 setTitle:@"India" forState:UIControlStateNormal];
    [myButton2 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:myButton2];

 [self.myScrollView setContentSize:CGSizeMake(160, 35)];
    _myScrollView.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.myScrollView];
}

-(void)ListViewAction:(id)sender
{

    if ([[sender currentTitle] isEqualToString:@"World"])
    {
        NSLog(@"successful ");            
        [tempArray addObject: worldArray];
    }
    else if ([[sender currentTitle] isEqualToString:@"India"])
    {
         NSLog(@"successful again");
        [tempArray addObject: IndiaArray];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tempArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
    {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]autorelease];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text=[tempArray objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewCont=[[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    [self presentViewController:detailViewCont animated:YES completion:nil];

}
4

1 回答 1

0

尝试使用下面的代码

在.h

NSMutableArray *tempArray;
NSMutableArray *worldArray;
NSMutableArray *IndiaArray;

  - (void)viewDidLoad
 {
   [super viewDidLoad];

   tempArray = [NSMutableArray alloc]init];
   worldArray=[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",nil];
   IndiaArray=[[NSMutableArray alloc]initWithObjects:@"abc",@"pqr",@"xyz", nil];
   [self PutButtoninScrollView:2];
  }



-(void)ListViewAction:(id)sender
 {

     if ([[sender currentTitle] isEqualToString:@"World"])
      {
        [tempArray removeAllObjects];
        [tempArray addObjectsFromArray:worldArray];

      }
    else if ([[sender currentTitle] isEqualToString:@"India"])
     {
        [tempArray removeAllObjects];
        [tempArray addObjectsFromArray: IndiaArray];
     }

  [yourTableView reloadData];
}
于 2013-02-18T10:09:27.327 回答