2010-10-31 75 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(特別是自定義單元格部分),並從一個更簡單的表格視圖單元格開始,一次添加一層複雜度,直至達到目標。

相關問題