- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30; //count number of row from counting array hear cataGorry is An Array
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *testword=@"pass";
if ([testword isEqualToString:@"pass"]) {
static NSString *MyIdentifier = @"cell1";
TextTableViewCell *cell ;
cell= [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell.lblText.text=[NSString stringWithFormat:@"textcelll= %i", indexPath.row+1];
cell.btnTextbox.tag=indexPath.row;
[cell.btnTextbox addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}else{
static NSString *MyIdentifier1 = @"cell2";
ImageTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];
return cell1;
}
}
-(void)customActionPressed:(UIButton*)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView1];
NSIndexPath *indexPath = [self.tableView1 indexPathForRowAtPoint:buttonPosition];
TextTableViewCell *cell = (TextTableViewCell*)[self.tableView1 cellForRowAtIndexPath:indexPath];
if (indexPath != nil)
{
int currentIndex = indexPath.row;
NSLog(@"currentIndex == %d",currentIndex);
int tableSection = indexPath.section;
NSLog(@"tableSection == %d",tableSection);
}
if (!cell.btnTextbox.isSelected ){
cell.btnTextbox.selected=YES;
[cell.btnTextbox setImage:[UIImage imageNamed:@"checkClick.png"] forState:UIControlStateNormal];
NSLog(@"button tag %i",cell.btnTextbox.tag);
NSLog(@"check click");
}else{
cell.btnTextbox.selected=NO;
[cell.btnTextbox setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
NSLog(@"check ");
}
in simulator , when i am clicked the button in first row (indexpath.row=0) then i am scrolling tableview, button click will auto display in 7th row (indexpath.row=6)
Question is ,i want to know , what happened in really and how to avoid this (when i'm scrolling)?
Since cells are being reused, you have to (in your cellForRowAtIndexPath) to set image to one state or another (to set one image or another).
What really happen is that you set Image for one cell but that cell is being reused through whole table. You use only 5(lets say that is how much you cells can fit into one screen) cells and when you load next you actually reuse the one with an image.
So, in your cellForRowAtIndexPath you will have to check if the button state is selected or not and then assign appropriate image.
For completness sake, I would also suggest you to use Accessory type as well as Xib files for these kind of cells. You can set it in storyboard under accessory dropdown or through code: