更改uitableview行内的元素会影响滚动中的其他行

时间:2015-04-09 17:43:19

标签: ios objective-c uitableview

我试图根据项目

更改我的uitableview行中的Uimage颜色

列表数组。例如,如果记录ID等于

中的数字之一

我的数组颜色会被更改。这是我的代码:

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", _ID_list_entries];
        BOOL result = [predicate evaluateWithObject:appRecord.ids];

        if(result)
        {
           UIButton *B_btn = (UIButton *)[sctvCell.contentView viewWithTag:504];
NSLog(@"self.ID_list_entries appRecord.ids : %@",appRecord.ids);
            CGSize targetSize = (CGSize){100,100};
            UIImage* image;
            {
                CGRect rect_2 = (CGRect){ .size = targetSize };

                UIGraphicsBeginImageContext( targetSize );
                {
                    CGContextRef X = UIGraphicsGetCurrentContext();

                    UIImage* uiGem = [UIImage imageNamed: @"graycolor_btn.png"];

                    // draw gem
                    [uiGem drawInRect: rect_2];

                    // overlay a red rectangle
                    CGContextSetBlendMode( X, kCGBlendModeColor ) ;
                    CGContextSetRGBFillColor ( X,  0.9, 0, 0,  1 );
                    CGContextFillRect ( X, rect_2 );

                    // redraw gem
                    [uiGem drawInRect: rect_2
                            blendMode: kCGBlendModeDestinationIn
                                alpha: 1. ];

                    image = UIGraphicsGetImageFromCurrentImageContext();
                }
                UIGraphicsEndImageContext();
            }


            [B_btn setImage:image forState:UIControlStateNormal];
        }

现在问题是当我滚动时这也会改变其他行。

我在my:

中使用此方法
- (void)configureCellXXX:(UITableViewCell *)sctvCell atIndexPath:(NSIndexPath *)indexPath{

在我的日志中还有一个

self.ID_list_entries appRecord.ids : 102

每次滚动。

1 个答案:

答案 0 :(得分:1)

问题出现是因为tableview正在重用该单元格。您需要通过覆盖自定义单元格中的prepareForReuse方法并将值重置为您想要默认的值来重置它。