当滚动表视图单元格到视图外时,单元格文本会更改 [英] When Scrolling a table view cell out of view, cell text changes

查看:38
本文介绍了当滚动表视图单元格到视图外时,单元格文本会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView and i programatically add two buttons to the cell. 1 button adds to the cells text ( counts up ) the other subtracts 1 (counts down). However, lets say i add 4, the cell's text will be 4, but when i scroll that cell up and out of the view, when it comes back down into view, the cells text is back to 1 which is where it started out. The same happens if i add(it does the same if i subtract also) to the cells text and switch pages and then go back to the table view. Here is the 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

Any and all help is appreciated! Thanks: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];

    }
}

推荐答案

您需要类似的内容,将值存储在单元格外部.这是因为细胞会被重复使用,并且不能长期保存.

You need something like this, where you are storing the values outside the cells. This is because the cells get reused and are not good long term storage.

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

这篇关于当滚动表视图单元格到视图外时,单元格文本会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆