自定义UITableViewCell随机改变值?

时间:2011-05-23 02:19:07

标签: objective-c xcode ipad uitableview

我遇到了问题。我有一个Custom UITableViewCel,单元格包含一个滑块,可以更改单元格上标签的值。单击单元格然后移动表格时,该值会在另一个单元格上复制,然后将值更改为其他单元格,将其值重置为0.

出于演示目的:

首先设置值

enter image description here

单击随机单元格然后返回:

一个完全不同的单元格,具有相同的数据但没有放在那里。

enter image description here

然后当返回到首次设置值的单元格时:

enter image description here

该值回到0

任何人都可以帮助我:

我的滑块值改变了代码;

labelSliderVal.text = [NSString stringWithFormat:@"%1.0f", sliderSlider.value];
if(sliderSlider.value < 30)
{
    self.backgroundColor = [UIColor redColor];
}
else if(sliderSlider.value > 60)
{
    self.backgroundColor = [UIColor greenColor];
} else {
    self.backgroundColor = [UIColor blueColor];
}

我的UITableViews didSelectRowAtIndexPath

已注释

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

}*/

的cellForRowAtIndexPath:

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"customCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:nil options:nil];
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        for(id currentObj in nibObjs)
        {
            if([currentObj isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObj;
            }

        }
    }

    GradeToolAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
    Module *aModule = [appDelegate.modules4 objectAtIndex:indexPath.section];
    AssessmentDetail *anAssess = [aModule.assessmentDetails4 objectAtIndex:indexPath.row];

    cell.sliderSlider.tag = indexPath.row;  
    cell.labelAssessment.text = [NSString stringWithFormat:@"%@", anAssess.assessmentName4];
    cell.labelAssessmentType.text = [NSString stringWithFormat:@"%@", anAssess.assessmentType4];
    cell.labelWeighting.text = [NSString stringWithFormat:@"%@", anAssess.assessmentWeighting4];
    cell.labelDueDate.text = [NSString stringWithFormat:@"%@", anAssess.assessmentDueDate4];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

初​​始化

NSMutableArray *results = [[NSMutableArray alloc] init];

NSInteger numberOfSections = [myTableView numberOfSections];
for ( int section = 0; section < numberOfSections ; section++ ) {
    NSInteger       numberOfRows   = [myTableView numberOfRowsInSection:section];
    NSMutableArray *sectionResults = [NSMutableArray array];

    [results addObject:sectionResults];

    for ( int row = 0; row < numberOfRows; row++ ) {
        [sectionResults addObject:[NSNumber numberWithFloat:0.0]];
    }
}

tableView:cellForRowAtIndexPath:

...
NSArray *sectionResults = [results objectAtIndex:indexPath.section];
NSNumber *number = [sectionResults objectAtIndex:indexPath.row];

slider.value = [number floatValue];

...

sliderValueChanged:

- (void)sliderValueChanged:(UISlider *)slider {
    CustomCell *cell = (CustomCell *)slider.superview;
    NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
    NSArray *sectionResults = [results objectAtIndex:indexPath.section];

    ...
    NSNumber *number = [NSNumber numberWithFloat:slider.value];
    cell.sliderValueLabel.text = [NSString stringWithFormat:@"%f", slider.value];

    [sectionResults replaceObjectAtIndex:indexPath.row withObject:number];
    ...
}

totalForSection:的代码,

- (float) totalForSection:(NSInteger)sectionIndex {
    NSArray *sectionResults = [results objectAtIndex:sectionIndex];
    float result = 0.0;

    for (NSNumber *number in sectionResults) {
        result += [number floatValue];
    }

    return result;
}

- (float)sumTotal {
    float result = 0.0;
    NSinteger numberOfSections = [myTableView numberOfSections];
    for ( int i = 0; i < numberOfSections; i++ ) {
        result += [self totalForSection:i];
    }

    return result;
}

初步回答

由于可重复使用的细胞,这种情况正在发生。您需要存储内容的状态并在tableView:cellForRowAtIndexPath:中相应地更新单元格。在滑块值更改时,

- (void)sliderValueChanged:(UISlider *)slider {
    CustomCell *cell = (CustomCell *)slider.superview;
    NSIndexPath *indexPath = [myTableView indexPathForCell:cell];

    AssessmentDetail *anAssessment = [module.assessmentDetails4 objectAtIndex:indexPath.row];
    anAssessment.property = slider.value;
    cell.propertyLabel.text = [NSString stringWithFormat:@"%f", slider.value];
}

现在模型和标签都已更改。因此,下次重复使用单元格时,标签应该得到正确的值。