2014-09-04 7 views
-1

Когда я прокручивать TableView дает этот тип перекрытия enter image description hereКогда Scroll UITableView это перекрываться имя IOS

Я пишу код, приведенный ниже в методе CellForRow AtIndexPath, я создаю TableView ... программно

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]]; 
UIImageView *view; 

if (indexPath.row % 2) { 
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]]; 
}else{ 
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]]; 
} 
cell.backgroundView=view; 

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; 
imgView.backgroundColor=[UIColor clearColor]; 
[imgView.layer setCornerRadius:25]; 
[imgView.layer setMasksToBounds:YES]; 
[imgView.layer setBorderColor:[UIColor whiteColor].CGColor]; 
[imgView setImage:[info objectForKey:@"thumbnail"]]; 
[cell.contentView addSubview:imgView]; 

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)]; 

[label setText:[info objectForKey:@"name"]]; 
[cell.contentView addSubview:label]; 

return cell; 

}

+0

Мы не волшебники и не экстрасенсы. Добавьте код и более подробную информацию. – n00bProgrammer

+4

Вы неправильно обработали повторное использование ячеек. разместите здесь свой код cellPathForRowAtIndexPath. – Yogendra

+0

Гораздо лучше. Извините, если раньше я казался грубым. Здесь проблема '[cell.contentView addSubview: label];'. Каждый раз, когда вызывается 'cellForRowAtIndexPath:', вы каждый раз добавляете новую метку. Поскольку ячейки повторно используются, ярлык, добавленный ранее, никогда не удаляется. – n00bProgrammer

ответ

7

Попробуйте этот код .Вы использовали dequeueReusableCellWithIdentifier:

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; 

if (cell == nil) 
{ 
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
+0

ОК я попробую –

+0

ОК, спасибо, он будет работать ... –

+0

вы можете решить мою другую проблему? –

-1
You can try with it. Just remove the previous contentView after create a table cell. 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell==nil) 
    { 
     cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    //****add this code*******// 
    for (UIImageView *v in [cell.contentView subviews]) 
    { 
     [v removeFromSuperview]; 
     NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView subviews]); 
    } 
    for (UILabel *v in [cell.contentView subviews]) 
    { 
     [v removeFromSuperview]; 
     NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView subviews]); 
    }///////// 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]]; 
    UIImageView *view; 
    if (indexPath.row % 2) 
    { 
     view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]]; 
    } 
    else 
    { 
     view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]]; 
    } 
    cell.backgroundView=view; 
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; 
    imgView.backgroundColor=[UIColor clearColor]; 
    [imgView.layer setCornerRadius:25]; 
    [imgView.layer setMasksToBounds:YES]; 
    [imgView.layer setBorderColor:[UIColor whiteColor].CGColor]; 
    [imgView setImage:[info objectForKey:@"thumbnail"]]; 
    [cell.contentView addSubview:imgView]; 

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)]; 

    [label setText:[info objectForKey:@"name"]]; 
    [cell.contentView addSubview:label]; 

    return cell; 
} 
+0

Просто добавьте этот код после создания ячейки. Я думаю, это поможет вам. для (UIImageView * v в [cell.contentView subviews]) { [v removeFromSuperview]; NSLog (@ "% @ hello \ n \ n \ n \ n \ n% @", v, [cell.contentView subviews]); } для (UILabel * v in [cell.contentView subviews]) { [v removeFromSuperview]; NSLog (@ "% @ hello \ n \ n \ n \ n \ n% @", v, [cell.contentView subviews]); } – Monikanta

+0

Вы знаете, что это не наука о ракетах, чтобы правильно форматировать ваш код. Я сделал это достаточно близко к каждому другому ответу, который вы дали, но теперь я просто собираюсь сделать снизу. Я удалю, как только вы правильно отформатировали свой ответ, чтобы он читался -1 – Popeye