自定义UITableViewCell不更新

时间:2011-11-02 10:16:40

标签: ios objective-c iphone uitableview cocoa-touch

我想在单元格前面设置复选标记。当我单击复选标记图标时,将调用正确的函数并更改数据源。

My Custom cell

我调用 [table reloadData] ,之后甚至还会调用 cellForRowAtIndexPath ,但它总是将旧表格单元格出列,而不是使用新数据源重新加载它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CheckboxCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

NSLog(@"cellForRowAtIndexPath row: %d", indexPath.row);

// use CustomCell layout 
CheckboxCell *checkboxCell;

if(cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CheckboxCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            checkboxCell =  (CheckboxCell *)currentObject;
            break;
        }
    }
} else {
    checkboxCell =  (CheckboxCell *)cell; // return cell;
}

Observation *observation = [observations objectAtIndex:indexPath.row];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd.MM.yyyy";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *nowString = [dateFormatter stringFromDate:observation.date];
[dateFormatter release];

NSString *amountString = [[NSString alloc] initWithFormat:@"%d", observation.amount];

checkboxCell.name.text = [observation.organism getNameDe];
checkboxCell.date.text = nowString;
checkboxCell.amount.text = amountString;
checkboxCell.latName.text = [observation.organism getLatName];

// Define the action on the button and the current row index as tag
[checkboxCell.checkbox addTarget:self action:@selector(checkboxEvent:) forControlEvents:UIControlEventTouchUpInside];
[checkboxCell.checkbox setTag:observation.observationId];

// Define the action on the button and the current row index as tag
[checkboxCell.remove addTarget:self action:@selector(removeEvent:) forControlEvents:UIControlEventTouchUpInside];
[checkboxCell.remove setTag:observation.observationId];

// Set checkbox icon
if(observation.submitToServer) {
    NSLog(@"CHECKED");
    checkboxCell.checkbox.imageView.image = [UIImage imageNamed:@"check_icon.png"];
} else {
    NSLog(@"UNCHECKED");
    checkboxCell.checkbox.imageView.image = [UIImage imageNamed:@"checkbox.gif"];
}

[amountString release];
[observation release];

return checkboxCell;


// Set checkbox icon
if(observation.submitToServer) {
    NSLog(@"CHECKED");
    checkboxCell.checkbox.imageView.image = [UIImage imageNamed:@"check_icon.png"];
} else {
    NSLog(@"UNCHECKED");
    checkboxCell.checkbox.imageView.image = [UIImage imageNamed:@"checkbox.gif"];
}

[amountString release];
[observation release];

return checkboxCell;

}

我认为我在cellForRowAtIndexPath中做错了什么..有人可以帮帮我吗?

修改

第一个问题可以解决(感谢Maggie)。现在,复选标记正在第一次正确更改。但不知何故,如果我在另一个单元格上更改它,它会崩溃在下一行:

- (void) checkboxEvent:(UIButton *)sender
{
UIButton *button = (UIButton *)sender;
NSNumber *number = [NSNumber numberWithInt:button.tag];

for(Observation *ob in observations) {

    if(ob.observationId == [number intValue]) { // <---- IT'S CRASHING HERE
        ob.submitToServer = !ob.submitToServer;

        NSLog(@"New value: %@", (ob.submitToServer) ? @"YES" : @"NO");
    }
}

[table reloadData];
}

但观察对象不是零。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

替换

 else {
    return cell;
}

部分

 else {
    checkboxCell =  (CheckboxCell *)cell;
 }

并使用适当的数据填充您的checkboxCell。