ios - 滚动tableview计数标签值是否改变?

标签 ios objective-c uitableview

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

cell = (PickAClassCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PickAClassCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
cell.CountLabel.tag=1000;
return cell;
} 
-(void)PlusButtonSelect :(UIButton *)sender
{
    UILabel *label1 = (UILabel *)[sender.superview viewWithTag:1000];
int count = [label1.text intValue];

        count = count + 1;

        label1.text = [NSString stringWithFormat:@"%d",count];

        NSLog(@"%@",label1);
}

这是我的代码,当我单击 PlusButtonSelect 时计数会增加。问题是当我的 tableview 滚动时,countlabel 变为零。

最佳答案

终于找到答案了,这里的myData是NsMutableArray。首先基于 No of Row 我们可以在 myData 中获取第一个零。 在 cellForRowAtIndexPath

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

     cell = (PickAClassCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

      if (cell == nil)
       {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PickAClassCell" owner:self options:nil];
         cell = [nib objectAtIndex:0];

        }

        cell.CountLabel.text=[NSString stringWithFormat:@"%@",[myData objectAtIndex:indexPath.row]];

        cell.CountLabel.tag=1000;
        [cell.PlusCount addTarget:self action:@selector(PlusButtonSelect :)forControlEvents:UIControlEventTouchUpInside];
        return cell;
}

-(void)PlusButtonSelect :(UIButton *)sender
{

  UILabel *label1 = (UILabel *)[sender.superview viewWithTag:1000];
    NSLog(@"%@",label1);
   int count = [label1.text intValue];
    count = count + 1;
   NSString *Strcount= [NSString stringWithFormat:@"%d",count];
  [myData replaceObjectAtIndex:sender.tag withObject:Strcount];
  [tableview reloadData];

}

Minusbutton 也一样。

关于ios - 滚动tableview计数标签值是否改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41057554/

相关文章:

iphone - 如何在 UILabel 中画一个圆圈?

ios - 在 heightForRowAtIndexPath 中推断行高

IOS 在 Swift 中获取通话分钟数

ios - 如何仅使用 GPU 将 CIImage 裁剪为多边形?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value - UITableView Add Button

iOS 8 : Background execution

ios - 检测 4x4 iOS 图 block 上的触地、触地、触摸移动事件

objective-c - MKannotationView 与 UIButton 作为 subview ,按钮不响应

ios - 在 tableview 中滚动时 UIActivityIndi​​cator 不会向上移动

ios - SWIFT tableView 行对选择没有反应