CaGradientLayer о странном поведении UItablecell - PullRequest
0 голосов
/ 18 июля 2011

Я вставил этот градиент в свой метод cellForRowAtIndexPath, но он делает странные вещи. когда я продолжаю прокручивать страницу вверх и вниз, она закрашивает ячейки снова и снова, поэтому страница становится все темнее и темнее. Должен ли я положить это где-то еще?

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

    //NSString *cellValue=[variable object ]
    // Configure the cell...

NSString *cellValue = [items objectAtIndex:indexPath.row];

if (indexPath.row==0){


    cell.textLabel.text=cellValue;
        //cell.textLabel.font=bold;
    cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:11];

    cell.textLabel.font = [UIFont boldSystemFontOfSize:(CGFloat)16];


}
else {

    cell.textLabel.text=cellValue;
        //cell.textLabel.font=bold;
    cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:14];

}

UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
UIColor *color = [UIColor colorWithHue:0.56f  saturation:0.98 brightness:0.65 alpha:0.5];
UIColor *colorWhite = [UIColor colorWithHue:0.0f  saturation:0.0 brightness:0.95 alpha:0.5];
UIColor *colorBlack = [UIColor colorWithHue:1  saturation:1 brightness:0 alpha:0.2];


myBackView.backgroundColor=color;
UIColor *textColor = [UIColor colorWithHue:0.0f  saturation:0.0 brightness:0.95 alpha:1];

cell.selectedBackgroundView = myBackView;
cell.selectedTextColor=textColor;
[myBackView autorelease];


UIView *cellView = [[UIView alloc] initWithFrame:cell.frame];
                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cellView.bounds;
                gradient.startPoint = CGPointMake(0, 0.5);
                gradient.endPoint = CGPointMake(1.0, 0.5);
                gradient.colors = [NSArray arrayWithObjects:(id)[colorBlack CGColor], (id)[colorWhite CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:0];

[cellView autorelease];


return cell;
}

Ответы [ 2 ]

3 голосов
/ 18 июля 2011

Ваш код выглядит не завершенным, но вы вставляете слой КАЖДЫЙ раз, когда табличное представление запрашивает ячейку.

2 голосов
/ 18 июля 2011

Вы вызываете код рисования каждый раз, когда ячейка появляется в представлении, когда вам нужно сделать это только тогда, когда она выделена и инициализирована. Переместитесь в скобки оператора if, и у вас больше не возникнет проблем.

Я знаю, потому что прошлой ночью сделал то же самое ...

Это относится к любым пользовательским меткам или любому другому рисунку, который вы, возможно, захотите сделать. Помимо этого, в операторе if используется любое значение для чего-либо еще, например, меток и цветов фона.

Удачи!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...