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

发布于 11-26 03:19 字数 3043 浏览 4 评论 0原文

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

I'm trying to add number from 1 to 9 from plist to each cell in UITableView. However the first view shows nothing and 1 is shown in the second cell, 2 is shown in the third cell and so on. I did some tests by adding NSLog(@"test"); inside if(cell==nil) but only 7 "test" is printed while there are 9 cells.. does that mean codes inside if(cell==nil) do not executed for the first and last cell?
Anybody care to solve this problem? :(

// 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;
}

Heres another method I added for subviews

//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;
}

Thank you
oh btw its simulator 4.2 that I'm using.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2024-12-03 03:19:54

第一次显示 UITableView 时,会对每个可见单元格调用一次 tableView:cellForRowAtIndexPath:。这是因为 UITableView 从重用池中回收单元并且为空,这就是 dequeueReusableCellWithIdentifier: 返回 nil 的原因。

您的代码将分配 7 个可见的单元格,这就是为什么“test”被打印 7 次。

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

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

The first time the UITableView is displayed, tableView:cellForRowAtIndexPath: is called once for every visible cell. This is because UITableView recycles cells from a reuse pool and is empty which is why dequeueReusableCellWithIdentifier: returns nil.
Your code will allocate the 7 cells that are visible, that is why 'test' is printed 7 times.

As you why '1' shows in the second cell, are you sure you don't have a blank item in the first row of your plist array?

Also, you should autorelease lblTemp, as it is retained by the parent view.

月朦胧 2024-12-03 03:19:54

特别是当您的问题围绕前几个单元格时,在 tableView:cellForRowAtIndexPath: 中设置断点,然后单步执行可疑行可能会帮助您了解发生了什么。

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

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

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

注意数据数组的实际值,您就会更清楚应用程序运行时的行为。

Especially as your problem is around the first few cells, a breakpoint in tableView:cellForRowAtIndexPath: followed by single stepping through the suspect lines will probably help you understand what is going on.

Click on the left margin of the code window at the first line of code in tableView:cellForRowAtIndexPath:, i.e., this one:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Then run the app in the simulator and when the debugger stops at the line, you should see two small windows at the bottom of your editor window. They show local variables on the left, and messages (your NSLog messages, for instance) on the right.

Now click on the little "jumping" arrow above the local variables to go forward one line of code at the time.

When you have seen enough, click on the button that looks like a play button on a media player, and normal execution resumes until the breakpoint is reached again - i.e., when the next line of the table is to be shown.

Pay attention to the actual values of the data arrays, and you'll be much wisesr as to what your app does when it is running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文