2010-10-31 76 views
0

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

我也试过用beginUpdatesendUpdates调用了包围reloadRowsAtIndexPaths:的调用,没有任何变化。

很明显,我不理解这里的根本。我究竟做错了什么?这是问题的方法:

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

}

+0

你还可以发布你的cellForRowAtIndexPath实现吗?我认为这可能与它有关。 – 2010-10-31 18:00:49

+0

好的,就在这里。这不是最漂亮的代码,我知道这些addSubviews正在创建一个大的内存泄漏。 – user1816236 2010-10-31 18:41:04

回答

1

的主要问题是,你似乎可以用在所有电池的单timeField参考。至少,timeField应该在每个单元格中分别创建(就像heatingSetPointField一样),然后使用它的标签从单元格中提取它。

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

我认为reloadData是“工作”,因为你没有尝试滚动表格视图向上/向下,当他们回到视图中看到单元格中的错误数据。

您可能想要阅读Table View Programming Guide(特别是自定义单元格部分),并从一个更简单的表格视图单元格开始,一次添加一层复杂度,直至达到目标。

相关问题