自定义 TableViewCell 随机更改值?

发布于 2024-11-09 13:15:39 字数 2964 浏览 0 评论 0原文

我有问题。我有一个自定义 UITableViewCel,单元格包含一个滑块,可以更改单元格标签上的值。单击单元格然后移动表格时,该值会在另一个单元格上复制自身,然后将该值更改为不同的单元格,并将其值重置为 0。

出于演示目的:

首先设置值

< img src="https://i.sstatic.net/cJjUn.png" alt="在此处输入图像描述">

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

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

在此处输入图像描述

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

在此处输入图像描述

值又回到 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;
}

I'm having a problem. I have a Custom UITableViewCel, the cell contains a slider which changes a value on a label on the cell. When clicking on the cell and then moving the table, the value replicates itself on another cell and then changes the value to a different cell, resetting it's value to 0.

For demonstration purposes:

First setting the value

enter image description here

Clicking on a random cell then returns:

A totally different cell with the same data that was not put there.

enter image description here

And then when returning back to the cell where the value was first set:

enter image description here

The value is back to 0

Can anyone help me here:

My Slider value changed code;

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];
}

And my UITableViews didSelectRowAtIndexPath

Commented out

/*
- (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-11-16 13:15:39

初始化

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];
}

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

Initialization

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]];
    }
}

In tableView:cellForRowAtIndexPath:,

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

slider.value = [number floatValue];

...

In 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];
    ...
}

Code for 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;
}

Initial Answer

Well this is happening because of reusable cells. You will need to store the state of the contents and update the cell accordingly in tableView:cellForRowAtIndexPath:. On slider value change,

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

Now both the model and the label have been changed. So next time cell is reused, the label should get the right value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文