在表中使用reloadRowsAtIndexPaths:时出现奇怪的更新

时间:2010-10-31 17:04:22

标签: objective-c iphone-sdk-3.0 uikit

我有一张桌子,其中单元格中的accessoryView按钮被自定义复选标记图标替换。如果启用了一对温度设置的时间间隔,则单元格行会显示复选标记以及时间,加热和冷却值。如果时间被禁用,则没有复选标记,并且timefield.text中显示的时间被修改为状态:“已禁用”。当我使用reloadData时,代码工作正常。但是,由于我一次只更新了一行,这对我正在使用的大表来说太过分了。我正在尝试使用reloadRowsAtIndexPaths:,但我得到的屏幕更新不仅会影响单击的单元格,还会影响单击的上一个单元格。我没有使用动画(UITableViewRowAnimationNone),但将其更改为淡化动画(UITableViewRowAnimationFade)以查看正在发生的事情。果然,衰落发生在所需的表行和不需要的表行上。此外,当前单元格行的时间值显示在上一个单元格行时间字段中。一旦我点击一行,就会有更新没有问题,但是一旦切换到另一行,我就会同时影响这两行。就像更新方法从某个地方捡起来一样。

我还尝试通过拨打reloadRowsAtIndexPaths:beginUpdates来调整endUpdates来电,并且没有任何变化。

显然,我不理解这里的一些基本内容。我究竟做错了什么?这是有问题的方法:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{   

  UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  UIButton *button = (UIButton *)cell.accessoryView;

  BOOL checked;

  if (intervalDisabled[((indexPath.section * 4) + indexPath.row)] == NO) {
    checked = YES;
    intervalDisabled[((indexPath.section * 4) + indexPath.row)] = YES;
  } else {
    checked = NO;
    intervalDisabled[((indexPath.section * 4) + indexPath.row)] = NO;
  }

  UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
  [button setBackgroundImage:newImage forState:UIControlStateNormal];

  timeField.text = [NSString stringWithFormat:@"%@", [self.dateFormatter stringFromDate:[timeOfDays objectAtIndex:((indexPath.section * 4) + indexPath.row)]]];

  //[self.tableView reloadData]; <-- This works fine

  NSArray *rowArray = [NSArray arrayWithObject:indexPath];

  [self.tableView reloadRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationFade];
}

编辑:

行。这是您要求的代码。我希望它有所帮助。我很乐意发布其他部分。对不起,我还没有在这个编辑器中格式化代码。

// Customized cell for setpoints

- (UITableViewCell *)setPointsCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     NSString * temperatureSymbol;

static NSString *kCustomCellID = @"setPointsCellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}

// Set up the cell. 
NSDictionary *dictionary = [listOfRows3 objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Content"];
NSString *cellValue = [array objectAtIndex:indexPath.row];

//Set properties of label and add its text
cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
cell.textLabel.text = cellValue;

// Add time field
timeField = [[UITextField alloc] init];
timeField.tag = (indexPath.section * 100) + (indexPath.row * 10) + TIME_FIELD;
CGRect frame = CGRectMake(80.0, 6.0, 90.0, 31.0);
timeField.frame = frame;
timeField.borderStyle = UITextBorderStyleRoundedRect;
timeField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//  timeField.backgroundColor = [UIColor clearColor];

timeField.textAlignment = UITextAlignmentRight;

// Check and handle situation where time interval is disabled
BOOL rowHasCheck;

if (intervalDisabled[((indexPath.section * 4) + indexPath.row)] == NO) {
    rowHasCheck = YES;
    timeField.text = [NSString stringWithFormat:@"%@", [self.dateFormatter stringFromDate:[timeOfDays objectAtIndex:((indexPath.section * 4) + indexPath.row)]]];
} else {
    rowHasCheck = NO;
    timeField.text = @"Disabled";
}

UIImage *image = (rowHasCheck) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect bFrame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = bFrame;  // match the button's size with the image size

[button setBackgroundImage:image forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;


// Override default keyboard handler so we can use a picker instead
timeField.delegate = self;

[cell.contentView addSubview:timeField];

// Set up termperature (F or C) to display
if (temperatureScale == FAHRENHEIT) {
    temperatureSymbol = @"F";
} else {
    temperatureSymbol = @"C";
}

// Add heating setpoint field
UITextField *heatingSetPointField = [[UITextField alloc] init];
heatingSetPointField.tag = (indexPath.section * 100) + (indexPath.row * 10) + HEATING_FIELD;

frame = CGRectMake(180.0, 6.0, 52.0, 31.0);
heatingSetPointField.frame = frame;
heatingSetPointField.borderStyle = UITextBorderStyleBezel;
heatingSetPointField.backgroundColor = [UIColor colorWithRed:1.0 green:0.2 blue:0.2 alpha:1.0];
heatingSetPointField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
heatingSetPointField.textAlignment = UITextAlignmentRight;

heatingSetPointField.text = [NSString stringWithFormat:@"%i %@", hSetpoints[((indexPath.section * 4) + indexPath.row)], temperatureSymbol];

// Override default delegate handler
heatingSetPointField.delegate = self;

[cell.contentView addSubview:heatingSetPointField];

// Add cooling setpoint field
UITextField *coolingSetPointField = [[UITextField alloc] init];
coolingSetPointField.tag = (indexPath.section * 100) + (indexPath.row * 10) + COOLING_FIELD;
frame = CGRectMake(240.0, 6.0, 52.0, 31.0);
coolingSetPointField.frame = frame;
coolingSetPointField.borderStyle = UITextBorderStyleBezel;
coolingSetPointField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
coolingSetPointField.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:1.0 alpha:1.0];
coolingSetPointField.textAlignment = UITextAlignmentRight;
coolingSetPointField.text = [NSString stringWithFormat:@"%i %@", cSetpoints[((indexPath.section * 4) + indexPath.row)], temperatureSymbol];

// Override default delegate handler
coolingSetPointField.delegate = self;

[cell.contentView addSubview:coolingSetPointField];

[timeField release];
[heatingSetPointField release];
[coolingSetPointField release]; 

return cell;

}

1 个答案:

答案 0 :(得分:1)

主要问题是您似乎在所有单元格中使用单个timeField引用。至少应该在每个单元格中单独创建timeField(就像heatingSetPointField一样),然后使用其标记从单元格中提取它。

还有许多其他问题(例如,当重复使用单元格时,文本字段将多次添加到每个单元格)。

我认为reloadData正在“正常工作”,因为您没有尝试向上/向下滚动表格视图,并在单元格返回视图时看到错误的数据。

您可能需要阅读Table View Programming Guide(尤其是自定义单元格部分)并从更简单的表格视图单元格开始,一次添加一层复杂性,直到达到目标为止。