将表格视图单元格滚动到视图外时,单元格文本会更改

时间:2011-11-29 01:16:45

标签: iphone ios cocoa-touch uitableview

我有一个UITableView,我以编程方式向单元格添加了两个按钮。 1个按钮添加到单元格文本(向上计数),其他减去1(向下计数)。但是,假设我添加4,单元格的文本将为4,但是当我向上滚动该单元格并将其从视图中移出时,当它返回到视图时,单元格文本将返回{{1}它开始的地方。如果我添加(如果我也减去它也会相同)到单元格文本和切换页面,然后返回到表视图,也会发生同样的情况。这是1

cellForRow

感谢任何和所有帮助!感谢:d

Here is the screen Shot of the cell

按钮的方法

    - (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;
}

1 个答案:

答案 0 :(得分:2)

你需要这样的东西,你将值存储在单元格之外。这是因为细胞被重复使用并且不能很好地长期储存。

- (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];
}