При прокрутке ячейки табличного представления текст ячейки изменяется - PullRequest
1 голос
/ 29 ноября 2011

У меня есть UITableView, и я программно добавляю две кнопки в ячейку.Кнопка 1 добавляет к ячейкам текст (считает вверх), остальные вычитает 1 (считает вниз).Однако, скажем, я добавляю 4, текст ячейки будет 4, но когда я прокручиваю эту ячейку вверх и из представления, когда она возвращается в вид, текст ячейки возвращается к 1, гдеэто началось.То же самое происходит, если я добавляю (он делает то же самое, если я тоже вычитаю) текст ячеек и переключаю страницы, а затем возвращаюсь к представлению таблицы.Вот cellForRow:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         newBtn = [[UIButton alloc]init];
         newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [newBtn setFrame:CGRectMake(260,20,55,35)];
         [newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [newBtn setTitle:@"-" forState:UIControlStateNormal];
         [newBtn setEnabled:YES];
         [cell addSubview:newBtn];

         subBtn = [[UIButton alloc]init];
         subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [subBtn setFrame:CGRectMake(200,20,55,35)];
         [subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [subBtn setTitle:@"+" forState:UIControlStateNormal];
         [subBtn setEnabled:YES];
         [cell addSubview:subBtn];
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
    cell.textLabel.text = [cells objectAtIndex:indexPath.row];

return cell;
}

Любая и вся помощь приветствуется!Спасибо: D

Here is the screen Shot of the cell

Методы для кнопок

    - (IBAction)addLabelText:(id)sender{    
    cell = (UITableViewCell*)[sender superview];    
    cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];
}  

- (IBAction)subtractLabelText:(id)sender
{
    cell = (UITableViewCell*)[sender superview];        
    if ( [[cell.textLabel text] intValue] == 0){ 
        cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +0];
    }
    else{
        cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] -1];
        //[myTableView reloadData];

    }
}

1 Ответ

2 голосов
/ 29 ноября 2011

Вам нужно что-то вроде этого, где вы храните значения вне ячеек. Это связано с тем, что клетки используются повторно и не годятся для длительного хранения.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {

         subBtn = [[UIButton alloc]init];
         subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [subBtn setFrame:CGRectMake(200,20,55,35)];
         [subBtn addTarget:self action:@selector(addLabelText:indexPath.row) forControlEvents:UIControlEventTouchUpInside];
         [subBtn setTitle:@"+" forState:UIControlStateNormal];
         [subBtn setEnabled:YES];
         [cell addSubview:subBtn];
    } 

    // we're loading the value from the array each time the cell is displayed.
    cell.textLabel.text = [cellLabelValues objectAtIndex:indexPath.row];

return cell;
}

 - (IBAction)addLabelText:(int)currentRow{    
     NSString *newValue = [NSString stringWithFormat:@"%d",[[[cellLabelValues objectAtIndex:currentRow] intValue] +1];
    // we update the value in the array since this is the source of the data for the cell
    [cellLabelValues replaceObjectAtIndex:currentRow withObject:newValue];
    // now reload to get the new value
    [myTableView reloadData];
}  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...