Вопрос

I have created a custom cell and added a uibutton on it. On tap of that button, Im setting that button selected which changes button's image.

-(IBAction)btnInfoPressed:(id)sender
{
    [btnInfo setSelected:YES];
}

The above method is in the custom cell class. Now when I scroll down, after some cells, some other cell's button is also selected even though I havent tapped that button.

Here is my cellforrowatindexpath method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CustomCell";
CustomCell *c = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (c == nil)
{
    c = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
}
c.selectionStyle = UITableViewCellSelectionStyleNone;
return c;
}

Any ideas what needs to be done about it?

Это было полезно?

Решение

(From my above comments:) You cannot use the cell to store the state of a row, because cells are reused. The table view allocates only a finite number of cells. If you scroll down, dequeueReusableCellWithIdentifier returns one of the existing cells that have become invisible. Therefore, you have to store the row's state in a data source and update the complete state of the cell (including the button's state) in cellForRowAtIndexPath.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top