我正在创建包含一些行的表视图数量。我必须使用复选框选择多行并使用按钮移动到下一个表格视图。问题是,当我从一个表格视图移动到另一个表格视图时,使用 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];
}
}