2010-12-14 2 views
0

У меня есть прокручиваемый стол. В каждой ячейке я рисую 3 UILabels. Первые несколько клеток выглядят нормально. Но когда я просматриваю таблицу, UILabels, похоже, рисует предыдущие UILabels. На нем, как на ярлыках в ячейке, уже есть старый ярлык, который был очищен. Возможно, я мог бы исправить это, нарисуя фоновый цвет по всей ячейке, каждый раз перерисовывая, но это все еще не объясняет эту странную проблему и почему ее происходит.Прокручиваемый стол с проблемой перерисовывания. Кажется, что очистка

Кто-нибудь знает, почему это происходит и какое решение может быть?

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

Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
[cell addSubview:teamName1Label]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
[cell addSubview:teamVersusLabel]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
[cell addSubview:teamName2Label]; 

return cell; 
} 

Большое спасибо -кода

ответ

4

Я думаю, что лучшим решением этой проблемы является определение этих меток в группа - (UITableViewCell *) reuseTableViewCellWithIdentifier: (NSString *) Идентификатор withIndexPath: (NSIndexPath *) indexPath метод

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
teamName1Label.tag = 1; 
[cell.contentView addSubview:teamName1Label]; 
[teamName1Label release]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
teamVersusLabel.tag = 2; 
[cell.contentView addSubview:teamVersusLabel]; 
[teamVersusLabel release]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
teamName2Label.tag = 3; 
[cell.contentView addSubview:teamName2Label]; 
[teamName2Label release]; 

return cell; 
} 

сейчас метод cellForRowAtIndexPath будет be--

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

    UITableViewCell *cell=nil; 

    static NSString *Identifier = @"Cell"; 
    cell = [theTableView dequeueReusableCellWithIdentifier:Identifier]; 
    if(cell == nil){ 
    cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath]; 
    } 

    Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1]; 
    label.text = aMatch.teamName1; 

    label = (UILabel *)[cell.contentView viewWithTag:2]; 
    label.text = @"V"; 

    label = (UILabel *)[cell.contentView viewWithTag:3]; 
    label.text = aMatch.teamName2; 

    return cell; 
    } 

Просто попробуйте code..Hope это помогает :)

0

Это клетка повторное использование вопрос. Код добавляет метки в ячейку каждый раз, когда вызывается cellForRowAtIndexPath (даже если ячейка не равна нулю).

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

Создайте и добавьте метки к ячейке в разделе if (cell == nil). При создании меток задайте метки на ярлыках.

Затем за пределами if извлеките этикетки с помощью их тега, используя viewWithTag, и установите их текст.

Смежные вопросы