cellForRowAtIndexPath中的ReusableCellWithIdentifier问题 [英] ReusableCellWithIdentifier issue in cellForRowAtIndexPath

查看:71
本文介绍了cellForRowAtIndexPath中的ReusableCellWithIdentifier问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个imageView和一个UILabel。对于ImageView我已经为它分配了asynch Image并且它工作正常,但是当我滚动表时,第一行和最后一行的标签重叠。

In my app I have one imageView and one UILabel. For ImageView I have assigned asynch Image to it and it works fine, But the first and Last row's Labels get overlapped when I scroll the Table.

这是我的代码:

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

    productObject=[appDelegate.productArray objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *nameLabel;
    if (cell == nil) 
    {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero     reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    }   

    else 
    {

        AsyncImageView* oldImage = (AsyncImageView*)[cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];
    }

    CGRect nameLabelRect = CGRectMake(70,80,150,15);
    nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];    
    nameLabel.text = [NSString stringWithFormat:@"%@",productObject.productname];
    nameLabel.textAlignment = UITextAlignmentCenter;
    nameLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    nameLabel.font = [UIFont boldSystemFontOfSize:15];
    nameLabel.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview: nameLabel];

    }


推荐答案

cell使用seprate标识符,如下所示

for every cell use seprate identifier something like below

 NSString *cellIdentifier = [NSString stringWithFormat:@"cell%i",indexpath.row];

或者只是不使用可重复使用只是将单元格和数据放入其中

or just dont use the reuseability simply make cell and data into them

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

UITableViewCell *cell = [[UITableViewCell alloc]init];
UIButton *favBtn = [UIButton buttonWithType:UIButtonTypeCustom];
favBtn.frame = CGRectMake(0, 0, 50, 50);
favBtn.backgroundColor = [UIColor clearColor];
favBtn.showsTouchWhenHighlighted = YES;
favBtn.tag = indexPath.row;

UIButton *arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtn.frame = CGRectMake(280, 26, 25, 25);
arrowBtn.backgroundColor = [UIColor clearColor];
arrowBtn.showsTouchWhenHighlighted = YES;
[arrowBtn setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];

UILabel *date = [[UILabel alloc]initWithFrame:CGRectMake(55, 26, 250, 25)];
date.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0]  size:15];
date.text = ann.title;
date.textColor = [UIColor blackColor];
date.backgroundColor = [UIColor clearColor];

[cell.contentView addSubview:favBtn];
[cell.contentView addSubview:date];
[cell.contentView addSubview:arrowBtn];


    return cell;
}

我不是专家,但第二个对我很有帮助希望是肝脏你也是

i am not an expert but the second one worked for me pretty well hope hepls u as well

这篇关于cellForRowAtIndexPath中的ReusableCellWithIdentifier问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆