【问题标题】:ios - Objective-c only the first cell of UITableView doesnt show the label I addedios - Objective-c 只有 UITableView 的第一个单元格不显示我添加的标签
【发布时间】:2023-03-08 07:57:01
【问题描述】:

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

【问题讨论】:

  • 听起来像是数组中从 0 开始的索引有问题...
  • 我也有同样的问题,而且可以肯定的是,我的数组在索引 0 处没有零或零。

标签: objective-c ios uitableview uilabel


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    尤其是当您的问题围绕前几个单元格时,tableView:cellForRowAtIndexPath: 中的断点然后单步遍历可疑行可能会帮助您了解正在发生的事情。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

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

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

    当您看够了之后,单击媒体播放器上看起来像播放按钮的按钮,然后继续正常执行,直到再次到达断点 - 即显示表格的下一行时。

    注意数据数组的实际值,你会很清楚你的应用在运行时会做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多