当我从一个表视图移动到另一个表视图时,复选框保持选中状态

时间:2013-10-17 04:49:36

标签: ios objective-c uitableview checkbox

我正在创建包含一些行的表视图的数量。我必须选中带有复选框的多行,然后使用按钮移动到下一个表格视图。问题是,当我从一个表视图移动到另一个表视图时,NEXT按钮选择行,即复选框保持不变。表示当我选择第二行表视图时,则所有表视图都选择了第二行。我想为不同的表视图获得不同的选择。这是我的代码,请帮助

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if(val==1)
        {
            checkedArr=[[NSMutableArray alloc] init];
            for (int i = 0; i<17; i++)
            {
                [checkedArr addObject:@"1"];
            }
            NSLog(@"Checked arr size %i",[checkedArr count]);

            return 17;
        }
        else if(val==2)
        {
            checkedArr=[[NSMutableArray alloc] init];
            for (int i = 0; i<13; i++)
            {
                [checkedArr addObject:@"1"];
            }
            return 13;
        }
        else if(val==3)
        {
            for (int i = 0; i<25; i++)
            {
                [checkedArr addObject:@"1"];
            }
            return 25;
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {


        NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil)
        {
            cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
      BOOL checked =  [[checkedArr objectAtIndex:indexPath.row] boolValue];
        UIImage *image = (checked) ? [UIImage imageNamed:@"white_bg.png"] : [UIImage imageNamed:@"tick.png"];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, 40, 30);
        button.frame = frame;   // match the button's size with the image size
        button.tag = indexPath.row;
        [button setBackgroundImage:image forState:UIControlStateNormal];

        // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
       [button addTarget:self action:@selector(checkButtonTapped:event:)forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;

        return cell;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
       [checkedArr removeObjectAtIndex:indexPath.row];

        [checkedArr insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];
       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton *button = (UIButton *)cell.accessoryView;
        UIImage *newImage = (checked) ? [UIImage imageNamed:@"tick.png"] : [UIImage imageNamed:@"white_bg.png"];
    }

    - (IBAction)nextButtonClicked:(id)sender
    {

       if(val==1)
       {       
           NSLog(@"Val is before %i",val);
           NSLog(@"Val is after %i",val); 
       }
        val++;

        if(val==2)
        {

            self.navigationItem.title=[NSString stringWithFormat:@"Few steps away...    (%i/10)",val];
                     listArray = [[NSMutableArray alloc] initWithObjects:@"Aries",@"Taurus",@"Gemini",@"Cancer",@"Leo",@"Virgo",@"Libra",@"Scorpio",@"Sagittarius",@"Capricorn",@"Acquarius",@"Pisces",@"I don't believe in Astrology", nil];
            titleForQuestion.text=@"My Sun Sign is...";
            BackBtn.hidden = NO;
            [self.questionTable reloadData];

        }

        else if(val==3)
        {


            self.navigationItem.title=[NSString stringWithFormat:@"Few steps away...    (%i/10)",val];


            listArray = [[NSMutableArray alloc] initWithObjects:@"Aerobics",@"Auto Racing",@"Baseball",@"Basketball",@"Billiards/Pool",@"Bowling",@"Cycling",@"Dancing",@"Football",@"Golf",@"Hockey",@"Inline Skating",@"Martial arts",@"Rugby",@"Running",@"Skiing",@"Soccer",@"Sumo Wrestling",@"Swimming",@"Tennis/Racquet Sports",@"Volleyball",@"Walking/Hiking",@"Weights/Machines",@"WWF",@"Yoga", nil];
            titleForQuestion.text=@"What sports/exercise you like ?";
            BackBtn.hidden = NO;
            NextBtn.hidden=NO;
            [self.questionTable reloadData];
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试这种方式......

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    int p=0;
    if (aTableView==tbl_1)
    {
        p= [arr_tb1 count];
    }
    else if(aTableView == tbl_2)
    {
      p= [arr_tb2 count];
    }

    return p;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView==tbl_1)
    {
  }
  else if (tableView==tbl_2
{ }
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==tbl_!)
    {
   }
  else if (tableView==tbl_2)
{ }

}

如果您有任何问题,请与我们联系。

答案 1 :(得分:0)

首先通过xib为表视图设置标识符。

在按钮操作中尝试此操作:

   if([tableView1 identifier] isEqualToString:@"XYZ"])
       {
         //Do for 1st table
       }
       if([tableView2 identifier] isEqualToString:@"ABC"])
       {
         //Do for 2nd table
       }

tableView1和tableView2是表格视图的出口。