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

我正在尝试从plist中添加1到9的数字到UITableView中的每个单元格。 然而,第一个视图没有显示任何内容,第一个视图显示在第二个单元格中,2显示在第三个单元格中,依此类推。 我通过添加NSLog(@"test");做了一些测试NSLog(@"test");if(cell==nil)里面if(cell==nil)但只有7个“test”被打印,而有9个单元格。这是否意味着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; } 

这是我为子视图添加的另一种方法

 //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个可见的单元格,这就是“test”打印7次的原因。

正如您在第二个单元格中显示’1’的原因,您确定在plist数组的第一行中没有空白项吗?

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

特别是当您的问题出现在前几个单元格中时,tableView中的断点:cellForRowAtIndexPath:然后单步执行可疑行可能会帮助您了解正在发生的事情。

在tableView:cellForRowAtIndexPath:的第一行代码中单击代码窗口的左边距,即这一个:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

然后在模拟器中运行应用程序,当调试器停在该行时,您应该在编辑器窗口的底部看到两个小窗口。 它们在左侧显示局部变量,在右侧显示消息(例如,您的NSLog消息)。

现在点击局部变量上方的小“跳跃”箭头,前进一行代码。

当你看够了,点击媒体播放器上看起来像播放按钮的按钮,恢复正常执行,直到再次到达断点 – 即,当要显示表格的下一行时。

注意数据数组的实际值,你会更清楚你的应用程序在运行时的作用。