Leanne
Leanne

Reputation: 687

ios - Objective-c only the first cell of UITableView doesnt show the label I added

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.

Upvotes: 2

Views: 5145

Answers (2)

Monolo
Monolo

Reputation: 18253

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.

Upvotes: 1

emp
emp

Reputation: 3478

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.

Upvotes: 1

Related Questions