【问题标题】:When Scrolling a table view cell out of view, cell text changes将表格视图单元格滚动到视图之外时,单元格文本会更改
【发布时间】:2011-11-29 01:16:45
【问题描述】:

我有一个 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

按钮方法

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

    }
}

【问题讨论】:

  • 那是因为单元格被重用了,你需要在 cellForRowAtIndexPath 中(重新)设置东西(标签、按钮),以确保在重用单元格时,单元格上的所有组件都是正确的。例如,如果您在单元格上有一个按钮,并且其文本逐行不同,则需要在 cellForRowAtIndexPath 中重置该文本;如果所有行的文本都相同,则显然没有什么可担心的。
  • 非常感谢彼得的帮助!你如何建议我重置cellForRowAtIndexPath 中的单元格文本?非常感谢! :D

标签: iphone ios cocoa-touch uitableview


【解决方案1】:

您需要这样的东西,将值存储在单元格之外。这是因为细胞会被重复使用,并且不适合长期储存。

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

【讨论】:

  • 另外,感谢您的帮助! :D 当你和彼得都说我必须设置改变的值时,你们是什么意思?再次感谢!
  • 我认为您的值存储在 cells 数组中,对吧?更改 cells 数组中的值后,调用 ReloadData 以更新表格的单元格。我认为这是核心答案。
  • 是的,没错!哦!天哪,我想我明白了!谢谢老兄,我现在就试试,让你知道结果如何!
  • 好的,两个按钮都有自己的方法。我在其中添加了[myTableView reloadData]; 行。然而,它所做的只是使按钮不起作用。我把它们放在错误的地方了吗?对不起所有的noobish lol。
  • 您不能使用表格的单元格来存储您的值。您需要将更新后的值放入一个数组中(您说您有一个名为 cells 的数组,对吗?)所以,只需更新 cells 数组中的值,然后调用 reloadData .如果这没有意义,我会发布一个示例。
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多