UITableView文本重叠

UITableView文本重叠,uitableview,Uitableview,我正在开发UITableView。我以编程方式在视图中添加了一个表,并通过添加UILable作为每个单元格的子视图来填充一些数据。表有21行。问题是,当我向上或向下滚动时,前两行和最后2或3行中的数据与其他行的数据重叠。例如,前两名投球手的数据与前两名击球手的数据重叠。任务是首先显示击球手列表,然后将两个单元格留空,然后在同一张表格中显示投球手列表。我的代码是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt

我正在开发UITableView。我以编程方式在视图中添加了一个表,并通过添加UILable作为每个单元格的子视图来填充一些数据。表有21行。问题是,当我向上或向下滚动时,前两行和最后2或3行中的数据与其他行的数据重叠。例如,前两名投球手的数据与前两名击球手的数据重叠。任务是首先显示击球手列表,然后将两个单元格留空,然后在同一张表格中显示投球手列表。我的代码是

- (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; 
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}          
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对象索引:indexath.row-[inningObject.battingLine计数]-2];//从零开始下一个NSArray
string1=cBowler.bowlerName;
string2=cBowler.wickets;
}          
cgrecta=CGRectMake(5,0320,38);
如果(indexPath.row<[inningObject.battingLine count]| | indexPath.row>=[ININNGOBJECT.battingLine count]+2){
CGRect string1Rect=CGRectMake(5,10,150,18);
UILabel*string1Label=[[UILabel alloc]initWithFrame:string1Rect];
string1Label.backgroundColor=[UIColor COLOR WITHRED:0.0绿色:0.0蓝色:0.0阿尔法:0.0];
string1Label.textAlignment=UITextAlignmentLeft;
string1Label.text=string1;
string1Label.font=[UIFont boldSystemFontOfSize:17];
[cell.contentView addSubview:string1Label];
[String1标签释放];
CGRect string2Rect=CGRectMake(240,10,70,18);
UILabel*string2Label=[[UILabel alloc]initWithFrame:string2Rect];
string2Label.backgroundColor=[UIColor COLOR WITHRED:0.0绿色:0.0蓝色:0.0阿尔法:0.0];
string2Label.textAlignment=UITextAlignmentLeft;
string2Label.text=string2;
string2Label.font=[UIFont boldSystemFontOfSize:17];
[cell.contentView addSubview:string2Label];
[String2标签释放];
}
返回单元;
}

您看到的问题是,您对两种不同类型的数据使用相同的
单元标识符。有两种方法可以解决此问题:

1.为2种不同类型的数据创建两个不同的单元标识符,即

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

2.你现在使用的让投球手有两个空位的方法不是最好的。您可以使用大单元格,并在其中安装4个子视图,其中2个用于投球手,2个用于击球手


我建议选择第二种

非常感谢您的回复。我将使用您推荐的第二个选项。:-)
CellIdentifier = @"Batters Cell";//for batters