tags:

views:

33

answers:

1

Hi I am working on UITableView. I have added a table in my view programmatically and populated it with some data by adding UILable as a subview of each cell. Table has 21 rows. Problem is that when i scroll up or down, the data in the first two rows and last 2 or 3 rows is overlapped with some other row's data. For example first two bowlers data is over lapped with first two batsmen data. Task is to first show a list of batsman then leave 2 cells empty and then show a list of bowlers in the same table. My code is

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     
if (cell == nil) {         
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];     
}          
NSString *string1;     
NSString *string2;               
  if(indexPath.row < [inningObject.battingLine count]){
    Batter *cBatter = [inningObject.battingLine objectAtIndex:indexPath.row];         
    string1 = cBatter.batterName;         
    string2 = cBatter.runs;         
  }else if(indexPath.row > [inningObject.battingLine count]+1){
    Bowler *cBowler = [inningObject.bowlingLine objectAtIndex: indexPath.row-[inningObject.battingLine count]-2]; // start from zero for next NSArray
    string1 = cBowler.bowlerName;
    string2 = cBowler.wickets;         
  }          
  CGRect a=CGRectMake(5, 0, 320, 38);     
  if(indexPath.row < [inningObject.battingLine count] || indexPath.row >= [inningObject.battingLine count]+2){          
    CGRect string1Rect = CGRectMake(5, 10, 150, 18);
    UILabel *string1Label = [[UILabel alloc] initWithFrame:string1Rect]; 
    string1Label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];      
    string1Label.textAlignment = UITextAlignmentLeft;
    string1Label.text = string1;
    string1Label.font = [UIFont boldSystemFontOfSize:17];
    [cell.contentView addSubview: string1Label];
    [string1Label release];
    CGRect string2Rect = CGRectMake(240, 10, 70, 18);
    UILabel *string2Label = [[UILabel alloc] initWithFrame:string2Rect];         
    string2Label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    string2Label.textAlignment = UITextAlignmentLeft;
    string2Label.text = string2;
    string2Label.font = [UIFont boldSystemFontOfSize:17]; 
    [cell.contentView addSubview: string2Label];
    [string2Label release];     
  }
return cell; 
}
A: 

You see the problem is you are using the same CellIdentifier for two different types of data. Two ways you could deal with this:

1.Create two different cell identifiers for the 2 different kinds of data i.e.

CellIdentifier = @"Bowlers Cell";//for bowlers

and

CellIdentifier = @"Batters Cell";//for batters

2.The approach you are using of having 2 cells empty for bowlers isnt the best. You could instead have big cell and fit in 4 subviews in it 2 for bowlers, 2 for the batters.

I'd recommend going with option 2.

domino
Thanks a lot for the reply domino. I am going with the second option you recommended. :-)
Aqueel