gpt4 book ai didi

iphone - cellForRowAtIndexPath 中的 ReusableCellWithIdentifier 问题

转载 作者:行者123 更新时间:2023-12-03 20:25:17 25 4
gpt4 key购买 nike

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

这是我的代码:

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

}

最佳答案

对于每个单元格使用单独的标识符,如下所示

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

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

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

我不是专家,但第二个对我来说非常有效,希望对你也有帮助

关于iphone - cellForRowAtIndexPath 中的 ReusableCellWithIdentifier 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com