gpt4 book ai didi

iphone - UITableView数据在滚动时多次显示

转载 作者:行者123 更新时间:2023-11-28 22:28:55 24 4
gpt4 key购买 nike

我有一个 UITableView,其中显示数据,但当它滚动时,数据 (UILabel) 要么消失,要么一遍又一遍地叠加在一起。每次我滚动每个单元格时都会交换数据。

这是我的cellForRowAtIndexPath:代码

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

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

// configure the cell's background
UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]];
[cell.contentView addSubview:gradient];

// configure the cell's label
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)];

// grab a reference to the label's text from the tableData
nameLabel.text = [name objectAtIndex:indexPath.row];
nameLabel.textColor = [UIColor blackColor];
nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12];
nameLabel.backgroundColor = [UIColor clearColor];

// set the autoReiszing mask -- this way if the label spills over the editing
// [icon?] then the text will trail off in ...
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[cell.contentView addSubview:nameLabel];

// configure the cell's label
UILabel *tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)];

// grab a reference to the label's text from the tableData
tableTextViewLbl.text = [message objectAtIndex:indexPath.row];
tableTextViewLbl.textColor = [UIColor blackColor];
tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10];
tableTextViewLbl.backgroundColor = [UIColor clearColor];
tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[cell.contentView addSubview:tableTextViewLbl];

// configure the cell's label
UILabel *tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)];

// grab a reference to the label's text from the tableData
tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row];
tableTimeStampViewLbl.textColor = [UIColor lightGrayColor];
tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7];
tableTimeStampViewLbl.backgroundColor = [UIColor clearColor];
tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[cell.contentView addSubview:tableTimeStampViewLbl];

// UIImageView *image;
// UIImage *image1=[UIImage imageNamed:@"rest.png"];
// image=[[UIImageView alloc]initWithImage:image1];
// image.frame=CGRectMake(10,30,40,30);
//
// [cell.contentView addSubview:image];
//


return cell;

}

最佳答案

每次将单元格加载/重新加载到 View 中时,您都会创建一个 UILabel 实例。这不是它的完成方式。相反 - 将 UILabel 作为属性(可能是 IBOutlet)添加到 UITableView 子类中,并在 cellForRowAtIndexPath: 中更改它。

所以你会有一个新类,继承自 UITableViewCell - 我们称它为 MyCustomCell。

在 MyCustomCell.h 中:

@interface MyCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end

MyCustomCell.xib会定义UILabel的位置和配置,当然需要关联nameLabel属性。

在 cellForRowAtIndexPath 中,无需实例化新的 UILabel,您只需引用 cell.nameLabel 并更改文本。请务必在界面生成器中为单元类定义一个 resuseIdentifier 并使用以下方法实例化它:

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (!cell) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];

}

关于iphone - UITableView数据在滚动时多次显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056073/

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