gpt4 book ai didi

objective-c - ios - Objective-c 只有 UITableView 的第一个单元格不显示我添加的标签

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:04 25 4
gpt4 key购买 nike

我正在尝试将 plist 中的 1 到 9 的数字添加到 UITableView 中的每个单元格。但是,第一个 View 什么也没显示,第二个单元格中显示 1,第三个单元格中显示 2,依此类推。我通过在 if(cell==nil) 中添加 NSLog(@"test"); 进行了一些测试,但是当有 9 个单元格时只打印了 7 个“test”。 . 这是否意味着 if(cell==nil) 中的代码不会针对第一个和最后一个单元格执行?
有人愿意解决这个问题吗? :(

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(@"hi");
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [self getCellContentView:CellIdentifier];

//add another textfield
NSString *rankpath = [[NSBundle mainBundle] pathForResource:@"Rankvalue" ofType:@"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:rankpath];
rankValue = [rank objectAtIndex:indexPath.row];
rankLabel = (UILabel *)[cell viewWithTag:1];

// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"ALBA" size:[@"25" intValue]]];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:filepath];

//transparent cell
cell.backgroundColor = [UIColor clearColor];
}
//label
rankLabel.text = rankValue;
//[rankLabel setFont:[UIFont fontWithName:@"austin power" size:[@"40" intValue]]];
//rankLabel.textColor = [UIColor redColor];

CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];

cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];

//cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];

return cell;
}

这是我为 subview 添加的另一种方法

//new method for different text in each cell
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier
{

CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);

UILabel *lblTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

//UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:@"austin power" size:40]];
lblTemp.textColor = [UIColor redColor];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];

return cell;
}

谢谢哦,顺便说一句,我正在使用它的模拟器 4.2。

最佳答案

第一次显示 UITableView 时,tableView:cellForRowAtIndexPath: 为每个可见单元格调用一次。这是因为 UITableView 从重用池中回收单元格并且是空的,这就是 dequeueReusableCellWithIdentifier: 返回 nil 的原因。 您的代码将分配 7 个可见的单元格,这就是打印 7 次“test”的原因。

你为什么在第二个单元格中显示“1”,你确定你的 plist 数组的第一行没有空白项吗?

此外,您应该自动释放 lblTemp,因为它由父 View 保留。

关于objective-c - ios - Objective-c 只有 UITableView 的第一个单元格不显示我添加的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6805669/

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