【问题标题】:After scroll, UILabel not updating in UITableViewCell滚动后,UILabel 未在 UITableViewCell 中更新
【发布时间】:2013-07-18 13:40:42
【问题描述】:

我在动态 UITableView 单元格中有一个标签 (quantitylbl) 和一个步进器。我可以在步进值更改时更新 UILabel ,但是当我滚动直到该单元格离开可见屏幕并再次向后滚动以显示标签时,然后在步进值更改时,标签没有得到更新。在日志中我可以看到更新的值,但标签没有更新。帮助表示赞赏。代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UIStepper *stepper;
    UILabel *quantitylbl;

    int sectionCount = 0;

    if ([indexPath section] == 1)  sectionCount = 10;  // Manage tags

    switch ([indexPath row])
    {
        case 2:
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell2"];
            cell.textLabel.text = @"Quantity per day";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            stepper =[[UIStepper alloc] initWithFrame:CGRectMake(200, 60, 40, 20)];
            [stepper setStepValue:0.25];
            [stepper addTarget:self action:@selector(stepperPressed:) forControlEvents:UIControlEventValueChanged];
            stepper.tag = indexPath.section;

            quantitylbl = [[UILabel alloc] initWithFrame:CGRectMake(160, 12, 40, 20)];

            if ([indexPath section] == 0)
                quantitylbl.text = [self.milk1 objectForKey:@"Quantity"]; //get value from dict
            else
                quantitylbl.text = [self.milk2 objectForKey:@"Quantity"];

            [stepper setValue:[quantitylbl.text doubleValue]];
            quantitylbl.tag = sectionCount + [indexPath row];

            [cell setAccessoryView:stepper];
            [cell addSubview:quantitylbl];

            break;

}

- (IBAction)stepperPressed:(UIStepper *)sender
{
    UILabel *label;
    UITableViewCell* cell =  [sender superview];
    NSString* strval = [NSString stringWithFormat:@"%.2f",sender.value];

    if (sender.tag == 0)
    {
        label = (UILabel *)[cell viewWithTag:2];
        [self.milk1 setValue:strval forKey:@"Quantity"];
        label.tag = 2;
    }
    else
    {
        label = (UILabel *)[cell viewWithTag:12];
        [self.milk2 setValue:strval forKey:@"Quantity"];
        label.tag = 12;  
    }

    if (label != nil)
    {
        // I see correct value here, also label is updated correctly. But when I scroll the
        // label out of screen and bring it back, the UILabel no more gets updated though
        // I see correct values here.    
        label.text = strval; 
    }
}

【问题讨论】:

    标签: ios objective-c uitableview uilabel uistepper


    【解决方案1】:

    我认为您使用了错误的方法。您应该只从 cellForRowAtIndexPath 方法更新数据。更改stepperPressed方法中的类变量self.milk1,并在tableview上调用reloadData方法。

    - (IBAction)stepperPressed:(UIStepper *)sender
    {
        NSString* strval = [NSString stringWithFormat:@"%.2f",sender.value];
    
        if (sender.tag == 0)
        {
            [self.milk1 setValue:strval forKey:@"Quantity"];
        }
        else
        {
            [self.milk2 setValue:strval forKey:@"Quantity"];
        }
        [self.tableView reloadData];
    }
    

    【讨论】:

    • 谢谢!这解决了我的问题,也解决了更简洁的代码:)
    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多