androider
androider

Reputation: 449

Having trouble in displaying UITableView

Please see

    - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:SimpleTableIdentifier] autorelease];




UILabel *playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)];
[playerHeading setBackgroundColor:[UIColor clearColor]];


NSString *str=[NSString stringWithFormat:@"Player %i",indexPath.row];
[playerHeading setText:str];
[playerHeading setFont:[UIFont boldSystemFontOfSize:15]];

if(indexPath.section%2==0){
    playerHeading.textColor=[UIColor redColor];
}
else {
    playerHeading.textColor=[UIColor blueColor];

}

UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 13.0f, 170.0f, 30.0f)];
[txt setBackgroundColor:[UIColor clearColor]];


//[txt setBackgroundColor:[UIColor whiteColor]];
[txt setTextColor:[UIColor grayColor]];
txt.delegate=self;
//[txt setBorderStyle:UIBorderStyle ]
[txt setText:@"Enter Player name"];
//txt.layer.cornerRadius=8.0f;
   //   txt.layer.masksToBounds=YES;
//txt.layer.borderColor=[[UIColor redColor]CGColor];
   //   txt.layer.borderWidth= 1.0f;

[cell.contentView addSubview:txt];

[cell.contentView addSubview:playerHeading];

[cell.contentView setAlpha:0.8f];
//ce]ll.textLabel.text = [listData objectAtIndex:row];
}
return cell;

}

I want to display the number in sequence like Player 0, Player 1, Player 2... but as I scroll the table i get numbers in random format like Player 1, Player 0 ,Player 3 Please help how to solve this issue.

Upvotes: 0

Views: 68

Answers (2)

Dor Bashan
Dor Bashan

Reputation: 136

try to close the {} of the if(cell == nil) earlier like here:

if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:SimpleTableIdentifier] autorelease]; 
}

Upvotes: 0

jrturton
jrturton

Reputation: 119242

You are adding the labels as new subviews all the time - this means you will have label above label above label, which is a waste of memory as well as a potential source of display issues.

When you create your cells (inside if (cell == nil) create and add the subviews at that point, and assign each one a tag. Then configure them all outside that loop. An example just with your playerHeading label:

UILabel *playerHeading;

if (cell == nil)
{
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:SimpleTableIdentifier] autorelease];
    playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)];
    [playerHeading setBackgroundColor:[UIColor clearColor]];
    [playerHeading setFont:[UIFont boldSystemFontOfSize:15]];
    [playerHeading setTag:1];
    [cell.contentView addSubview:playerHeading];
}
else
{
    playerHeading = [cell.contentView viewWithTag:1];
}

playerHeading.text = [NSString stringWithFormat:@"Player %i",indexPath.row];

You do appear to have more than one section in your table (you have an if statement on indexPath.section) so your player numbers will start again from 0 in each section with the above code.

Upvotes: 1

Related Questions