0

我有一个集合视图,其中每个单元格包含 7 个按钮(通过代码而不是故事板创建)。

它们最初很清晰,但是如果我向上/向下滚动几次,质量就会下降。

当我改变视图并返回时,清晰度会恢复。

有任何想法吗 ?

添加:

我正在循环中制作这样的按钮(可以是 1 到 7 个按钮)

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"patientCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    Patient *aPt = [self.fetchedResultsController objectAtIndexPath:indexPath];
    PatientCVCell *ptCell = (PatientCVCell *) cell;
    ptCell.ptName.text = aPt.name;
    ptCell.ptRoom.text = aPt.room;
    ptCell.ptRx.text = aPt.diagnosis;

    int xPos = 20;
    NSArray *daysForRx = aPt.ofList.listDays;
    // loop through to add button for each day of Rx

    for (int i = 0; i < [daysForRx count]; i++) {
        // get the treatment day that == postition in array

        for (Treatment *t in aPt.patientRx) {
            if (t.day == daysForRx[i]) {
                //NSLog(@"%i", xPos);
                StatusButton *testButton = [StatusButton buttonWithType:UIButtonTypeCustom];
                testButton.frame = CGRectMake(xPos, 110, 28, 28);
                testButton.btnTreatment = t;
                // match status of the RX to the correct button

                if ([t.status intValue] == NotSeen) {
                    [testButton setImage:[UIImage imageNamed:@"toSee"] forState:UIControlStateNormal];
                    testButton.linkNumber = NotSeen;
                }
                else if ([t.status intValue] == SeenNotCharted) {
                    [testButton setImage:[UIImage imageNamed:@"seenNotCharted"] forState:UIControlStateNormal];
                    testButton.linkNumber = SeenNotCharted;
                }
                else if ([t.status intValue] == SeenCharted) {
                    [testButton setImage:[UIImage imageNamed:@"seenCharted"] forState:UIControlStateNormal];
                    testButton.linkNumber = SeenCharted;
                }
                else if ([t.status intValue] == NotSeeing) {
                    [testButton setImage:[UIImage imageNamed:@"notSeeing"] forState:UIControlStateNormal];
                    testButton.linkNumber = NotSeeing;
                }
                else if ([t.status intValue] == NotSeeingDC) {
                    [testButton setImage:[UIImage imageNamed:@"notSeeingDischarged"] forState:UIControlStateNormal];
                    testButton.linkNumber = NotSeeingDC;
                }
                [testButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:testButton];
                xPos = xPos + 36;
            }
        }
    }
    return cell;
}

图像大小正确,因此无需缩放图像。

发生在模拟器和设备上。


再仔细看,里面的图像很清晰!所以这个问题与我在方形按钮中的圆形按钮的透明度有关!

4

1 回答 1

0

您正在使一个单元格出列,然后将您的按钮添加到出列的单元格中。

这些按钮永远不会被删除。当您向上和向下滚动时,屏幕外的单元格将被放入出队队列。此时它们仍然有按钮,然后它们被出列并添加更多按钮。你有很多按钮,这就是为什么它看起来很模糊,你的内存占用变得更大。

我会从单元格内部添加按钮。将它们保存在一个数组中,以便以后删除它们。然后我会添加一个方法来设置您需要的按钮数量。像这样:

// header
@property (strong, nonatomic) NSMutableArray *buttons;

// implementation
- (void)setNumberOfButtons:(NSInteger)numberOfButtons withTarget:(id)target selector:(SEL)selector {
    // remove existing buttons from view
    [self.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
    // "save" existing buttons in a reuse queue so you don't have to alloc init them again
    NSMutableArray *reuseQueue = self.buttons;

    self.buttons = [NSMutableArray arrayWithCapacity:numberOfButtons];
    for (NSInteger i = 0; i < numberOfButtons; i++) {
        UIButton *button = [reuseQueue lastObject];
        if (button) {
            [reuseQueue removeLastObject];
        }
        else {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            // you should always use the same target and selector for all your cells. otherwise this won't work. 
            [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        }
        [self.buttons addObject:button];
        button.frame = ....
        // don't set up images or titles. you'll do this from the collectionView dataSource method
    }
}

然后,您将设置按钮的数量collectionView:cellForItemAtIndexPath:并根据您的需要配置每个按钮。类似的东西:

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = ... dequeue ...
    Object *object = ... get from your backend ...
    /* configure your cell */
    if ([cell.buttons count] != object.numberOfItems) {
        // no need to remove and add buttons if the item count stays the same
        [cell setNumberOfButtons:object.numberOfItems withTarget:self selector:@selector(buttonPressed:)];
    }
    for (NSInteger i = 0; i < [object.numberOfItems count]; i++) {
        UIButton *button = cell.buttons[i];
        [button setImage:... forState:UIControlStateNormal];
    }
}

动作看起来像这样:

- (IBAction)buttonPressed:(UIButton *)sender {
    UICollectionView *collectionView;
    CGPoint buttonOriginInCollectionView = [sender convertPoint:CGPointZero toView:collectionView];
    NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:buttonOriginInCollectionView];
    NSAssert(indexPath, @"can't calculate indexPath");
    Cell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    if (cell) {
        NSInteger pressedButtonIndex = [cell.buttons indexOfObject:sender];
        if (pressedButtonIndex != NSNotFound) {
            // do something
        }
    }
    else {
        // cell is offscreen?! why?
    }
}

很直接。获取indexPath,获取collectionViewCell,查看按下的按钮有哪个索引

于 2013-09-03T04:18:38.733 回答