开发者

Double selection of a uitableviewcell in Custom Cell

开发者 https://www.devze.com 2023-03-27 00:06 出处:网络
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 whe

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.


Try this:

NSArray *arrayNibs = [[NSBundle mainBundle] loadNibNamed:@"InterestsTableViewCell" owner:self options:nil];
for (id currentObject in arrayNibs){
    if ([currentObject isKindOfClass:[UITableViewCell class]]){
        cell =  (InterestsTableViewCell *) currentObject;
        cell.Delegate = self;
        cell.total = [dataArray count];
        break;
    }
}

and let me know if this works for you.


Quick Fix - change this line from

InterestsTableViewCell *cell = (InterestsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

to

InterestsTableViewCell *cell = nil;

Its not good from a memory/efficiency perspective because you won't be reusing the cells, instead will be reloading one from nib each time - but it will demonstrate whether what Florian Mielke is saying is correct (and his recommended approach will provide a better solution long term).

0

精彩评论

暂无评论...
验证码 换一张
取 消