Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

iphone - ReusableCellWithIdentifier issue in cellForRowAtIndexPath

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.

Here is my code:

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...