gpt4 book ai didi

ios - UITableViewCell 交换中的 UILabel 文本值

转载 作者:行者123 更新时间:2023-11-28 18:35:34 29 4
gpt4 key购买 nike

UILabel 每次加载时 UITableViewCell 中的文本值都会被交换。因为我的 cell height 是 140 cell.TextLabel。 text 无法设置 frame 并使其显示在中心。但是我需要在单元格的开头显示 TitleText。所以,我使用了 UILabel 根据需要显示。但每次我加载单元格时,它显示这些单元格的错误标题。但是当我记录该数组时,它会正确记录,如果我检查 cell.TextLabel.text 它会显示正确。但不在 UILabel 中。很困惑。无法追踪我哪里出错了。在此先感谢。以下是我的代码。

`

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 140;
}


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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Title;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Title = [[UILabel alloc]init ];
Title.frame =CGRectMake(20, 18, 152, 23);
[Title sizeToFit];
}
NSMutableArray *contentArray = [sectionDict valueForKey:[array objectAtIndex:indexPath.section]];
NSLog(@"content %@",contentArray);
Title.text = [contentArray objectAtIndex:indexPath.row];
[cell addSubview:Title];
UIImageView *seaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PacificOcean.png" ]];
seaImage.frame = CGRectMake(20, 60, 280, 80);
[cell addSubview:seaImage];
return cell;
}

`

最佳答案

将您的 subview Labelimage 添加到 cell == nil block 中的单元格为每个 subview 设置标签。通过标签访问 block 外的 subview 。希望对您有所帮助。

if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *Title = [[UILabel alloc]init ];
Title.tag = 1000;
Title.frame =CGRectMake(20, 18, 152, 23);
[Title sizeToFit];
[cell.contentView addSubview:Title];

UIImageView *seaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PacificOcean.png" ]];
seaImage.frame = CGRectMake(20, 60, 280, 80);
[cell.contentView addSubview:seaImage];
}
NSMutableArray *contentArray = [sectionDict valueForKey:[array objectAtIndex:indexPath.section]];
UILabel *Title = (UILabel *)[cell.contentView viewWithTag:1000];
Title.text = [contentArray objectAtIndex:indexPath.row];
return cell;

像这样更改您的代码..

关于ios - UITableViewCell 交换中的 UILabel 文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19830483/

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