i have an issue regarding UitableViewCell. the issue is that i have made a custom cell and i have a check box image button in that cell. i check and uncheck it. it works fine but the issue is that when i select Row # 1. it also selects row # 10 same goes with other rows like for 2 it will auto select row # 11. i am showing 10 rows at a time.
here is my code for CellForIndexPath
static NSString *cellIdentifier = @"Cell";
InterestsTableViewCell *cell = (InterestsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *arrayNibs = [[NSBundle mainBundle] loadNibNamed:@"InterestsTableViewCell" owner:self options:nil];
cell = [arrayNibs objectAtIndex:0];
cell.delegate = self;
cell.total = [dataArray count];
}
cell.tag = indexPath.row;
cell.lblTitle.text = [dataArray objectAtIndex:indexPath.row];
if (indexPath.row==0) {
cell.imgBg.image = [UIImage imageNamed:@"tableCell_top_default.png"];
}else if(indexPath.row == [dataArray count]-1){
cell.imgBg.image = [UIImage imageNamed:@"tableCell_bottom_default.png"];
}else{
cell.imgBg.image = [UIImage imageNamed:@"tableCell_middle_default.png"];
}
return cell;
Plus i am detecting touch so that i can change the background image (i have an other image as background). Code for Touch Began (in custom cell is below)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//NSLog(@"%i",tag);
isSelected = !isSelected;
btnTickMark.selected = !btnTickMark.selected;
(isSelected ? (onImage = YES) : (onImage = NO));
Can somebody help me in this issue that why 2 rows are selected when i click a row.
Thanks in advance
This is because UITableView dequeues the cell. As you are showing 10 rows per screen it totally fits, that row 11 appears checked as it is dequeued from row 1.
To avoid that behavior you have to add a check to
tableView:cellForRowAtIndexPath:to set your checked parameter for each cell – like the text you set for every cell.