本文介绍了ios - Objective-c 只有 UITableView 的第一个单元格不显示我添加的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Thank youoh btw its simulator 4.2 that I'm using.

推荐答案

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

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.

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

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?

另外,你应该自动释放 lblTemp,因为它被父视图保留.

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

这篇关于ios - Objective-c 只有 UITableView 的第一个单元格不显示我添加的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:57